如何通过在Java中查询DB来填充对象构造函数



我试图通过查询数据库来创建UserInfo对象构造函数,并且我不断获得UserInfo thisUserInfo = new UserInfo()部分的cannot find symbol错误。我试图采取ResultSet rs,并在构造函数中填写,使一个"会话"的用户登录。

我在这里做错了什么?下面是我的代码:

private UserInfo getUserInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException {
    String userName = request.getParameter("userName");
    String nullString = null;
    char nullChar = ' ';
    ResultSet rs = null;
    stmt = conn.createStatement();
    String getInfoSQL = "SELECT * FROM " + studentsTable + " WHERE USERNAME = '" + userName + "'";
    rs = stmt.executeQuery(getInfoSQL);
    if(rs.next()) {
        UserInfo thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email));
        rs.close();
        stmt.close();
    } else {
        UserInfo thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString);
        rs.close();
        stmt.close();
    }
    return thisUserInfo;
}
这是我的UserInfo类:
public class UserInfo {
private final String userName;
private final String passWord;
private final String lastName;
private final String firstName;
private final String age;
private final char sex;
private final String email;
public UserInfo(String userName, String passWord, String lastName, String firstName, String age, char sex, String email) {
    this.userName = userName;
    this.passWord = passWord;
    this.lastName = lastName;
    this.firstName = firstName;
    this.age = age;
    this.sex = sex;
    this.email = email;
}
public String getUserName() {
    return this.userName;
}
public String getPassWord() {
    return this.passWord;                
}
public String getLastName() {
    return this.lastName;
}
public String getFirstName() {
    return this.firstName;
}
public String getAge() {
    return this.age;    
}
public char getSex() {
    return this.sex;
}
public String getEmail() {
    return this.email;
}

}

您需要为初学者发布UserInfo类的代码。我敢打赌,你没有定义一个构造函数来接受这些形参。

编辑你正试图隐式地从String转换为char,我怀疑你做不到。不要用rs.getString(sex),试试rs.getChar(sex)。或者,您可以添加一个额外的构造函数,该构造函数接受String作为sex参数。

编辑哦,我现在看到问题了。您从未定义任何其他变量,如passWord等。这些需要是用于列名的字符串或用于列索引的int。

同样,这是不相关的,但永远不要在这样的查询中传递未经处理的输入。你正在为SQL注入敞开大门。您应该使用预处理语句或者至少要转义输入

我不是百分百了解一切,但这对我来说是错误的…

if(rs.next()) {
    // UserInfo is declared within a local context
    UserInfo thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email));
    rs.close();
    stmt.close();
} else {
    // UserInfo is declared within a local context
    UserInfo thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString);
    rs.close();
    stmt.close();
}
// thisUserInfo does not exist
return thisUserInfo;

应该更像……

UserInfo thisUserInfo = null;
if(rs.next()) {
    thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email));
    rs.close();
    stmt.close();
} else {
    thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString);
    rs.close();
    stmt.close();
}
return thisUserInfo;

请注意,我个人更喜欢....

try {
    stmt = conn.createStatement();
    String getInfoSQL = "SELECT * FROM " + studentsTable + " WHERE USERNAME = '" + userName + "'";
    rs = stmt.executeQuery(getInfoSQL);
    UserInfo thisUserInfo = null;
    if(rs.next()) {
        thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email));
    } else {
        thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString);
    }
} finally {
    try {
        rs.close();
    } catch (Exception exp) {
    }
    try {
        rs.close();
    } catch (Exception exp) {
        stmt.close();
    }
}
return thisUserInfo;

正如(现在)所指出的……UserInfo期望sex参数为char,但您传递给它的是String

你应该尝试使用String sexValue = rs.getString(sex)之类的东西从数据库中提取值,如果它不是null,使用sexValue.getCharAt(0)

我也不知道userName, passWord, lastName, firstName, age, sexemail在哪里定义。

相关内容

  • 没有找到相关文章

最新更新