2016-07-24 2 views
-1

HSQLDBを使用してDBからデータを抽出する際の問題を解決するのに役立つ必要があります。 私はエラーを取得する:コードのjava.sql.SQLException:無効なカーソル状態:識別子カーソルがUPDATE、DELETE、SET、またはGETステートメントの行に配置されていません。 ResultSet is empty

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); 
    } 
} 

答えて

3

あなたのコード内の2つのミスは、限り私はそれを見ることができるように、あります。

まず、SQLクエリを正しく作成していません。それをデータベースに送る前に印刷してみてください。

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

間違いを見つけますか?

middleNameと(この場合は)Fredという文字列が等しいMY_TABLEからすべての行を選択しています。もちろん、これらの文字列は等しくないので、一致する行はありません。任意の引用符がmiddlenameを中心に、もはや存在しないことを

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

注ので、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. 
     } 
+0

:あなたの代わりに書きたい何

は、次のようなものです。ありがとうございました。 – user6631554

関連する問題