我的JSP页面包含以下用于登录认证的代码:
<%
System.out.println("Entered");
String username=request.getParameter("username");
String password=request.getParameter("password");
Connection con=getConnection.getConnectionBuilder();
PreparedStatement pstmt=con.prepareStatement("Select password from users where username=? and password=?");
pstmt.setString(1, "username");
pstmt.setString(2, "password");
boolean ifTrue=pstmt.execute();
System.out.println(""+ifTrue);
if (ifTrue==false)
{
out.print("Invalid userid and password combination");
}
%>
现在的问题是"当我输入无效的用户名和密码组合时,查询从Oracle返回no rows selected
,我无法理解如何才能捕获此消息。我试过使用布尔,但正如预期的那样,它正在返回true
。我找不到任何返回字符串和executeUpdate返回整数的pstmt方法
请尝试下面的代码。
PreparedStatement pstmt = con.prepareStatement("Select password from users where username=? and password=?");
pstmt.setString(1, "username");
pstmt.setString(2, "password");
ResultSet resultSet = pstmt.executeQuery();
if(!resultSet.next())
out.print("no rows selected");
}