我正在开发一个Android应用程序,我有一个登录视图,我想检查ResultSet是否返回没有匹配,在按钮下面写一个警告文本。但是我听说ResultSet永远不会返回null值。那我该怎么用?这是我的简短代码:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> users = new ArrayList<String>();
final String getUser = "SELECT username FROM userList WHERE username=?";
try {
pst = conn.prepareStatement(getUser);
pst.setString(1, String.valueOf(txtUsername));
rs = pst.executeQuery();
while (rs.next()) {
if(rs == null) { //change this line
txtAlert.setText("ERROR: No match!");
}
else {
users.add(rs.getString("username"));
}
}
final String getUserName = users.get(1);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
答案 0 :(得分:1)
您可以查看已添加的内容
while (rs.next()) {
users.add(rs.getString("username"));
}
if (users.isEmpty()) {
// not found!
}