java Java和mysql查询检查结果是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13829159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:06:43  来源:igfitidea点击:

Java and mysql query check if result is empty

javamysql

提问by hamchi

ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
if (zoeken.getRow() == 0){
   System.out.println("hi");
}
while( zoeken.next() ){
   System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
}

I am using this to check if the result zoekenis empty, but it throws an exception saying its illegal to do that regardless if it works or not.

我正在使用它来检查结果zoeken是否为空,但它会抛出一个异常,表示无论它是否有效,这样做都是非法的。

What is a better solution to check if result is empty or not

检查结果是否为空的更好解决方案是什么

回答by PermGenError

// I am using this to check if the result"zoeken" is empty, but it throws an exception saying its illegal to do that. regardless if it works or not

// 我用它来检查结果“zoeken”是否为空,但它抛出一个异常,说这样做是非法的。不管它是否有效

Don't invoke ResultSet.getRow()before you invoke ResultSet.next().

在调用ResultSet.next()之前不要调用ResultSet.getRow()

From API:

来自API:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

ResultSet 对象维护一个指向其当前数据行的游标。最初光标位于第一行之前。next 方法将光标移动到下一行,因为当 ResultSet 对象中没有更多行时它返回 false,所以可以在 while 循环中使用它来遍历结果集。

       ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
       boolean val = zoeken.next(); //next() returns false if there are no-rows retrieved 
        if(val==false){
            System.out.println("zoken is empty"); //prints this message if your resultset is empty
         }
        while(val){// only runs when there are rows in the resultset
           System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
          val=zoeken.next(); //updating val again to check if there are rows in result set
        }

If your result set is emptyzoeken.next()will return falsein which case your while loop will not execute.

如果您的结果集为zoeken.next()将返回false在这种情况下您的 while 循环将不会执行。

An Advice

一个建议

use PreparedStatementInstead of simple Statement. simple statement would lead to SQL Injectionand also would ease the pain for writing complex queries.Below is the sample code which use PreparedStatement.

使用PreparedStatement而不是简单的 Statement。简单的语句会导致SQL 注入,也会减轻编写复杂查询的痛苦。下面是使用 PreparedStatement 的示例代码。

String yourquery ="Select whatever From table where col1= ? and col2 =?"
PreparedStatement stmnt = Conn.preparedStatement(yourquery);
stmnt.setString(1, "val1");
stmnt.setString(2, "val2");

回答by Jigar Joshi

Use next()method

使用next()方法

boolean hasNext = zoeken.next();

回答by Sandaru

Following code will work for you

以下代码将为您工作

if(!zoeken.first() && !zoeken.next())//if fist and next records are empty
{
System.out.println("Result set is empty");
}
else
{
System.out.println("Result set is not empty");
}

回答by flungo

I know the OP probably doesn't need the info any more but if someone stumbles across this, I would agree with everything @PermGenError has said although, I feel a more elegant solution for the given code but using prepared statements would be:

我知道 OP 可能不再需要这些信息,但如果有人偶然发现了这一点,我会同意 @PermGenError 所说的一切,但我觉得给定代码的解决方案更优雅,但使用准备好的语句是:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM leden WHERE naam = ?");
ps.setString(title);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
    do {
        System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
    } while (rs.next());
} else {
    System.out.println("hi");
}

I have written this freehand so excuse any mistakes or if it doesn't compile. I just feel this is clearer and cuts down on the usage of local variables. If there is at least one row, go print the row and keep doing this until there are no more rows otherwise say "hi". But I assume you would want to say something more useful than "hi" maybe a "No results found" message or something.

我是徒手写的,所以请原谅任何错误或者它不能编译。我只是觉得这更清晰并且减少了局部变量的使用。如果至少有一行,请打印该行并继续执行此操作,直到没有更多行,否则说“嗨”。但我假设你想说一些比“嗨”更有用的东西,也许是“没有找到结果”的消息或其他东西。

回答by Olaf Dietsche

According to next()you must first use next and then getRow()

根据next()你必须先使用 next 然后 getRow()

ResultSet zoeken = stat.executeQuery("SELECT * FROM leden WHERE naam = '" + text + "'");
while( zoeken.next() ){
    int rowNum = zoeken.getRow();
    System.out.println(zoeken.getString(1) + zoeken.getString(2) + zoeken.getString(3));
}

回答by Srujan Kumar Gulla

without using Result Set next() you cant check the size or resultSet period.

如果不使用结果集 next(),您将无法检查大小或结果集周期。

A ResultSet cursor is initially positioned before the first row.

ResultSet 游标最初位于第一行之前。