我需要帮助来解决与使用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);
}
}
答案 0 :(得分:3)
据我所知,你的代码中有两个错误。
首先,您没有正确构建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
现在被解释为列名而不是字符串。如果这是您想要的,请删除columnName
变量任一侧的SQL字符串中的单引号。
第二个问题在这里:
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.
}