java.sql.SQLException:游标状态无效:标识符游标未定位在 UPDATE、DELETE、SET 或 GET 语句中的行上:;结果集为空

Posted

技术标签:

【中文标题】java.sql.SQLException:游标状态无效:标识符游标未定位在 UPDATE、DELETE、SET 或 GET 语句中的行上:;结果集为空【英文标题】:java.sql.SQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty 【发布时间】:2016-07-24 13:38:23 【问题描述】:

我需要帮助来解决与使用 HSQLDB 从数据库中提取数据相关的问题。 我得到错误:

java.sql.SQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getCurrent(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getColumnInType(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getString(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getString(Unknown Source)
    at DBactionSearch.actionPerformed(DBactionSearch.java:109)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hsqldb.HsqlException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 42 more

代码的问题部分是: text1 = rs.getString("firstname");

当我使用像SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE 这样的简单SQL 表达式时,一切正常,但是当我通过在前面提到的WHERE (SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE WHERE '" + columnName + "' = '" + search + "'; ") 部分添加变量使其变得更复杂时,出现错误。

这是类的完整代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBactionSearch implements ActionListener 

    JTextField textField1;
    JTextField textField2;
    JTextField textField3;
    public JTextField textField4;
    JTextField textField5;
    JTextField textField6;
    JTextArea textArea;
    String text1;
    String text2;
    String text3;
    String text4;
    String text5;
    String totalText;
    JComboBox comboBox;
    String search;

    public void getTextField1(JTextField textField1) 
        this.textField1 = textField1;
    

    public void getTextField2(JTextField textField2) 
        this.textField2 = textField2;
    

    public void getTextField3(JTextField textField3) 
        this.textField3 = textField3;
    

    public void getTextField4(JTextField textField4) 
        this.textField4 = textField4;
    

    public void getTextField5(JTextField textField5) 
        this.textField5 = textField5;
    

    public void getTextField6(JTextField textField6) 
        this.textField6 = textField6;
    

    public void setTextArea(JTextArea textArea) 
        this.textArea = textArea;
    

    public void actionPerformed(ActionEvent e) 

        String e1 = textField1.getText();
        String e2 = textField2.getText();
        String e3 = textField3.getText();
        String e4 = textField5.getText();
        String e5 = textField6.getText();
        final String columnName = textField4.getText();

        System.out.println("columnName = " + columnName);

        switch (columnName) 
        case "firstname":
            search = e1;
            break;
        case "middlename":
            search = e2;
            break;
        case "lastname":
            search = e3;
            break;
        case "emailaddress":
            search = e4;
            break;
        case "phonenumber":
            search = e5;
            break;
        default:
            System.out.println("Error");
        

        System.out.println("search = " + search);

        try 
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:///c:/hsqldb/mydb", "SA", "");

            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery(
                    "SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE WHERE '"
                            + columnName + "' = '" + search + "'; ");

            rs.next();
            // while (rs.next()) 
            text1 = rs.getString("firstname");
            text2 = rs.getString("middlename");
            text3 = rs.getString("lastname");
            text4 = rs.getString("emailaddress");
            text5 = rs.getString("phonenumber");
            // 

            // while (rs.next()) 
            // System.out.println(rs.getRow() + ". " + rs.getString("TITLE")
            // + "\t" + rs.getString("AUTHOR"));
            // 
        

        catch (Exception excepton) 
            excepton.printStackTrace();
        
        JFrame frame3 = new JFrame("Search result");
        frame3.setSize(350, 350);
        frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame3.setLocation(500, 250);
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 1));
        frame3.add(panel2);

        JTextArea textArea = new JTextArea();
        panel2.add(textArea);
        textArea.setVisible(true);

        totalText = " '" + text1 + "'   \n   '" + text2 + "'  \n   '" + text3 + "' \n" + " '" + text4 + "'  \n  '"
                + text5 + "' ";
        System.out.println("totalText = " + totalText);
        textArea.setText(totalText);

        frame3.setVisible(true);
    

【问题讨论】:

【参考方案1】:

据我所知,您的代码中有两个错误。

首先,您没有正确构建 SQL 查询。在将其发送到数据库之前尝试将其打印出来。你会发现你得到了类似下面的东西:

SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE WHERE 'middlename' = 'Fred';

发现错误了吗? (向右滚动。)

您正在从MY_TABLE 中选择所有行,其中字符串middleName 和(在这种情况下)Fred 相等。当然,这些字符串是不相等的,所以你不会得到匹配的行。

您可能想要运行以下查询:

SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE WHERE middlename = 'Fred';

请注意,middlename 周围不再有任何引号,因此 middlename 现在被解释为列名而不是字符串。如果这是您想要的,请删除 SQL 字符串中 columnName 变量两侧的单引号。

第二个问题在这里:

        rs.next();

您不必费心检查您是否得到任何结果。通过从这个方法返回false,HSQL 告诉你没有数据。但是,您选择不听这个并继续读取数据。

你想写的是如下内容:

        if (rs.next()) 
            // read the data out of the result set.
         else 
            // handle the case that no matching data was found.
        

【讨论】:

这有帮助。谢谢。

以上是关于java.sql.SQLException:游标状态无效:标识符游标未定位在 UPDATE、DELETE、SET 或 GET 语句中的行上:;结果集为空的主要内容,如果未能解决你的问题,请参考以下文章

java.sql.SQLException: - ORA-01000: 超过最大打开游标

java.sql.SQLException: - ORA-01000: 超过最大打开游标

java.sql.SQLException: - ORA-01000: 超过最大打开游标

超出最大游标 SQLException--配置问题或游标泄漏?

java.sql.SQLException:游标状态无效:标识符游标未定位在 UPDATE、DELETE、SET 或 GET 语句中的行上:;结果集为空

java.sql.SQLException: ORA-01000: 超出打开游标的最大数