Мне нужна помощь для решения проблемы, связанной с извлечением данных из БД с использованием HSQLDB. я получаю сообщение об ошибке:java.sql.SQLException: недопустимое состояние курсора: курсор идентификатора, не расположенный в строке в UPDATE, DELETE, SET или GET:; ResultSet пуст
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");
Когда я использую простое выражение SQL, как SELECT firstname, middlename, lastname, emailaddress, phonenumber FROM MY_TABLE
все работает, но когда я сделать его более сложным путем добавления переменных в части 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);
}
}
Это помогло. Спасибо. – user6631554