为什么视图返回null?提前谢谢。
这是我的观点jsp
String abb = "ab";
String cdd = "cd";
UserPass user = UserDb.selectUser(abb,cdd);
out.println(user);
这是我的UserPass类
public class UserPass {
String username;
String password;
public UserPass(){
this.username ="";
this.password ="";
}
public void setUserName(String username) {
this.username = username;
}
public String getUserName(){
return username;
}
}
最后是数据包UserDb
String query = "select user_name from UserPass where user_name = ? AND user_password = ? " ;
try{
ps = connection.prepareStatement(query);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.getString("user_name") != null) {
UserPass up = new UserPass();
up.setUserName(rs.getString("user_name"));
return up;
} else {
return null;
}
}
我的数据库很简单,条目有user_name和user_password。视图返回null
答案 0 :(得分:2)
您需要使用rs.next()来检查当前行是否有效:
rs = ps.executeQuery();
if (rs.next()) {
UserPass up = new UserPass();
up.setUserName(rs.getString("user_name"));
return up;
} else {
return null;
}