[英]Java jdbc preparedstatements how can I check for empty row
I am implementing a forgot_password type feature on my application and I can not find how to check if I have any rows returned. 我在我的应用程序上实现了forgot_password类型的功能,但找不到如何检查是否返回任何行。 The user first enters his/her email and then I search the database if the email is found proceed otherwise stop and tell them their email has not been found;
用户首先输入他/她的电子邮件,然后我搜索数据库(如果找到了该电子邮件),然后继续操作,并告诉他们未找到他们的电子邮件; This is my code
这是我的代码
Connection dbConnection=null;
dbConnection= DB.getConnection();
PreparedStatement preparedStatement= dbConnection.prepareStatement("Select email,password from profiles where email=?");
preparedStatement.setString(1, User_Email);
preparedStatement.executeQuery();
// This code is not working below empty null rows still say email found
if(preparedStatement==null)
{
return ok("Email not found");
}
else
{
return ok("Email Found");
}
The User_Email string works correctly and I have verified but it seems that User_Email字符串可以正常工作,并且我已经验证了,但是似乎
preparedStatement==null does nothing prepareStatement == null不执行任何操作
Nah, you're going about it the wrong way. 不,您正在以错误的方式进行操作。
executeQuery()
returns a ResultSet
. executeQuery()
返回一个ResultSet
。 You just need to check if ResultSet has rows. 您只需要检查ResultSet是否包含行。
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
//example
String email = rs.getString(1);
String pw = rs.getString(2);
}
else{
//no results!!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.