当表中有数据时,rs.next()返回false。使用oracle作为数据库。
try {
ps=con.prepareStatement("select * from users where uname=? and pass=?");
ps.setString(1, uname);
ps.setString(2, pass);
rs=ps.executeQuery();
out.println(rs.next());
if (rs.next()) {
out.println("<font color='green'>Successfull login</font>");
} else {
out.println(uname+" "+pass);
out.println("<font color='red'>Wrong username or password.</font>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:1)
致电
out.println(rs.next());
它移动到下一个结果,然后第二次调用它,下一个结果被称为
如果SQL中有1个结果,则在调用rs.next()的if语句中返回false
答案 1 :(得分:-1)
您正在使用ResultSet对象资源:
通过out.println(rs.next());
因此要重复使用,您必须添加
rs.previous();
之后
通过out.println(rs.next());
像
try {
ps=con.prepareStatement("select * from users where uname=? and pass=?");
ps.setString(1, uname);
ps.setString(2, pass);
rs=ps.executeQuery();
out.println(rs.next());
rs.previous();
if (rs.next()) {
out.println("<font color='green'>Successfull login</font>");
} else {
out.println(uname+" "+pass);
out.println("<font color='red'>Wrong username or password.</font>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}