简体   繁体   English

Java JDBC PreparedStatement我如何检查空行

[英]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.

相关问题 坚持PreparedStatements是否昂贵? (Java和JDBC) - Is it expensive to hold on to PreparedStatements? (Java & JDBC) 如果preparedStatements可以根据object而改变,如何正确优化JDBC代码? - How to correctly optimize JDBC code if the preparedStatements can change depending on the object? 如何使用java检查MongoDB中的字符串数组是否为空? - How can I check if an array of strings is empty in MongoDB with java? 不关闭我的JDBC PreparedStatements会导致内存泄漏吗? - Do I get a memory leak by not closing my JDBC PreparedStatements? 我如何排序带有空行的Java JTable并强制空行始终为最后? - How can i sort java JTable with an empty Row and force the Empty row always be last? 如何检查JTable JAVA中是否存在行? - How can I check if a row exists in JTable JAVA? 如何检查Java JDBC代码使用正确的列名? - How can I check that Java JDBC code uses the right column names? 我对如何使用 Java 和 Derby sql 服务器使用 PreparedStatements 感到困惑 - I am confused on how to use PreparedStatements using Java and Derby sql server jdbc preparedStatements为IN内的多个值 - jdbc preparedStatements for multiple value inside IN Java如何检查2d数组中的行是否为空。 - Java how to check if a row is empty in a 2d array.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM