0

我正在尝试通过查询数据库来创建一个 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;
}

}

4

2 回答 2

2

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

编辑您正在尝试从 a 隐式转换为Stringachar我怀疑您做不到。而不是rs.getString(sex)尝试rs.getChar(sex)。或者,您可以添加一个额外的构造函数,该构造函数接受 aString作为 sex 参数。

编辑哦,我现在看到了这个问题。您从未定义任何其他变量,例如passWord等。这些变量必须是列名的字符串或列索引的整数。

此外,这是不相关的,但永远不要在这样的查询中传递未经处理的输入。您正在为SQL Injection 敞开心扉。您应该改用准备好的语句, 或者确保至少转义输入。

于 2013-04-19T01:09:26.827 回答
1

我对所有事情都不是100%,但这对我来说似乎是错误的......

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期望参数是a charsex但您传递的是aString

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

我也不知道userName, passWord, lastName, firstName, age,sexemail是在哪里定义的。这让我有点担心……

于 2013-04-19T01:20:50.570 回答