Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 结果集代码不为';我不会被处决_Java_Mysql_Sql_Database_Resultset - Fatal编程技术网

Java 结果集代码不为';我不会被处决

Java 结果集代码不为';我不会被处决,java,mysql,sql,database,resultset,Java,Mysql,Sql,Database,Resultset,我有以下查询数据库的代码!但是while循环中的代码不会被执行!没有消息框,只是无法执行!谁能帮帮我!结果集不是空的!当我从try-catch块中打印出相同的值时,它会被执行并打印出正确的值!Th DB connection是一个标准的MySQL DB连接类 database = new DBConnection(); String dept = txtSearch.getText(); String Query = "SELECT * FROM department wher

我有以下查询数据库的代码!但是while循环中的代码不会被执行!没有消息框,只是无法执行!谁能帮帮我!结果集不是空的!当我从try-catch块中打印出相同的值时,它会被执行并打印出正确的值!Th DB connection是一个标准的MySQL DB连接类

database = new DBConnection();

    String dept = txtSearch.getText();
    String Query = "SELECT * FROM department where dept_name= '" + dept + "'";

    ResultSet set = database.queryDatabase(Query);

    try {
        if (set.next() == false) {
            JOptionPane.showMessageDialog(null, "No Matchs found for the search query! Try Again.", "Search Error", JOptionPane.ERROR_MESSAGE);
        } else {
            while (set.next()) {
                System.out.print(set.getString("dept_name"));
                txtName.setText(set.getString("dept_name"));
                txtDes.setText(set.getString("dept_desc"));
            }
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), ex.getCause().toString(), JOptionPane.ERROR_MESSAGE);
    }

通过调用
set.next()
,然后忽略此行中的数据,您将抛出查询的第一行:

    if (set.next() == false) {  // ***** here on this line
        JOptionPane.showMessageDialog(null, "No Matchs found for the search query! 
            Try Again.", "Search Error", JOptionPane.ERROR_MESSAGE);
    } else {
        while (set.next()) {
            System.out.print(set.getString("dept_name"));
            txtName.setText(set.getString("dept_name"));
            txtDes.setText(set.getString("dept_desc"));
        }
    }
相反,每次调用
next()
并返回true时,请确保从结果集中提取信息

您可以这样做:

int setCount = 0;
while (set.next()) {
  setCount++;
  System.out.print(set.getString("dept_name"));
  txtName.setText(set.getString("dept_name"));
  txtDes.setText(set.getString("dept_desc"));
}
if (setCount == 0) {
  // show a warning to the user that the result set was empty
}

通过调用
set.next()
,然后忽略此行中的数据,您将抛出查询的第一行:

    if (set.next() == false) {  // ***** here on this line
        JOptionPane.showMessageDialog(null, "No Matchs found for the search query! 
            Try Again.", "Search Error", JOptionPane.ERROR_MESSAGE);
    } else {
        while (set.next()) {
            System.out.print(set.getString("dept_name"));
            txtName.setText(set.getString("dept_name"));
            txtDes.setText(set.getString("dept_desc"));
        }
    }
相反,每次调用
next()
并返回true时,请确保从结果集中提取信息

您可以这样做:

int setCount = 0;
while (set.next()) {
  setCount++;
  System.out.print(set.getString("dept_name"));
  txtName.setText(set.getString("dept_name"));
  txtDes.setText(set.getString("dept_desc"));
}
if (setCount == 0) {
  // show a warning to the user that the result set was empty
}

谢谢!:)虽然我想要支票条件,但我找到了另一种绕过它的方法!:)谢谢大家!@不客气!如何检查结果集是否为空?我不检查结果集。而是检查文本字段是否为空!谢谢!:)虽然我想要支票条件,但我找到了另一种绕过它的方法!:)谢谢大家!@不客气!如何检查结果集是否为空?我不检查结果集。而是检查文本字段是否为空!