2017-02-06 60 views
0

我是h2數據庫的最近用戶,我需要一些SQL語法的幫助。無法將數據從tableView設置爲textField - 在Mouseclick和向上和向下箭頭鍵(H2數據庫)

我能夠從h2 dB中檢索數據並將其設置爲JavaFX tableView,在執行鼠標點擊或按鈕操作時(向下箭頭),預期的行爲是將當前的數據行從tableView顯示到文本框,下面是代碼。

,我發現了以下異常:

Invalid value "1" for parameter "parameterIndex" [90008-193] 

我敢肯定,這個異常是由於SQL語法獨特的H2數據庫,佔位(' "+slnoField.getText()+" ' ")工作在其他數據庫的罰款。請你能建議正確的語法或解決方案。非常感謝。

@FXML 
    public void UpdateTable(){ 
     data.clear(); 
     try 
     { 
      conn = lrconn.getDatabaseConnection(); 
      String sql = "SELECT * from APP_TABLE ; 
      pst = conn.prepareStatement(sql); 
      rs = pst.executeQuery(); 
      while(rs.next()) 
      { 

      data.add(new TestPOJO(
       rs.getString("SLNO"), 
       rs.getString("NAME") 
      )); 
       Table.setItems(data); 
      } 
      pst.close(); 
      rs.close(); 
     } 
     catch(Exception e1) 
     { 
      e1.printStackTrace(); 
     } 

     Table.setOnMouseClicked((MouseEvent me) ->{ 
        try{ 
         conn = lrconn.getDatabaseConnection(); 
         TestPOJO user = (TestPOJO)Table.getSelectionModel().getSelectedItem(); 
       String sql = "SELECT * from APP_TABLE where SLNO =' "+slnoField.getText()+" ' "; 
         pst = conn.prepareStatement(sql); 
         pst.setString(1, user.getSLNO()); 
         rs = pst.executeQuery(); 

         while(rs.next()){ 
         slnoField.setText(rs.getString("SLNO")); 
         nameField.setText(rs.getString("NAME")); 
         } 
         rs.close(); 
         pst.close(); 
        }catch(SQLException ex){ 
         Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
        } 

       } 
    ); 

     Table.setOnKeyReleased((KeyEvent e) ->{ 
        if(e.getCode() == KeyCode.UP || e.getCode() == KeyCode.DOWN){ 
         try{ 
          TestPOJO user = (TestPOJO)Table.getSelectionModel().getSelectedItem(); 
          String sql = "SELECT * from APP_TABLE where SLNO =' "+slnoField.getText()+" ' "; 
          pst = conn.prepareStatement(sql); 
          pst.setString(1, user.getSLNO()); 
          rs = pst.executeQuery(); 
          while(rs.next()){ 
           slnoField.setText(rs.getString("SLNO")); 
           nameField.setText(rs.getString("NAME")); 

         catch(IOException | SQLException ex){ 
          Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); 
         } 
        } 
       } 
    ); 
    } 

回答

0

你沒有正確使用PreparedStatement。將?放置在您想要在查詢字符串中插入參數的位置。你似乎沒有添加其中之一:

String sql = "SELECT * from APP_TABLE where SLNO = ?"; 
pst = conn.prepareStatement(sql); 
pst.setString(1, user.getSLNO()); 
rs = pst.executeQuery(); 
+0

非常感謝Fabian,忽略了這一點! – Berch

相關問題