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

时间:2013-04-19 01:00:17

标签: java resultset

我正在尝试通过查询数据库来创建UserInfo对象构造函数,并且我不断收到cannot find symbol部分的UserInfo thisUserInfo = new UserInfo()错误。我正在尝试使用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;
}

}

2 个答案:

答案 0 :(得分:2)

您需要为初学者发布UserInfo课程的代码。我敢打赌,你没有定义带有这些参数的构造函数。

修改您尝试隐式转换为Stringchar,我怀疑您无法做到。而不是rs.getString(sex)尝试rs.getChar(sex)。或者,您可以添加一个额外的构造函数,该构造函数接受性别参数的String

编辑哦,我现在看到了这个问题。你从来没有定义任何其他变量,比如passWord等。那些需要是列名的字符串或列索引的整数。

此外,这是无关的,但绝不会在类似的查询中传递未经过类型化的输入。你为SQL Injection打开了自己。你应该使用prepared statements instead  或者确保你至少逃避输入。

答案 1 :(得分:1)

我不是百分之百关于一切,但这对我来说是错误的......

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期望char参数的sex,但您传递String

您应该尝试使用String sexValue = rs.getString(sex)之类的内容从数据库中提取值,如果它不为空,请使用sexValue.getCharAt(0)

我也不知道userNamepassWordlastNamefirstNameagesexemail的位置定义。这让我有点担心......