2015-11-21 82 views
0

我有我的項目,它是一個駕駛執照數據庫。java.sql.SQLException:參數索引超出範圍(1>參數數量,爲0)ERROR

當用戶填寫字段時,這是正常的,但是當用戶按下「提交」按鈕時,一切都搞亂了。

The error Parameter index out of range錯誤顯示,所以這是我的代碼。

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {            

    String sql = "INSERT INTO dr_record_table (Driver's ID, Last Name, First Name, Middle Name, Street, Barangay, City/Municipality, Province, Country, Month, Day, Year, Gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

    try { 
     pst = conn.prepareStatement(sql); 
     pst.setString(1, idTxtFld.getText()); 
     pst.setString(2, lnTxtFld.getText()); 
     pst.setString(3, fnTxtFld.getText()); 
     pst.setString(4, mnTxtFld.getText()); 
     pst.setString(5, saTxtFld.getText()); 
     pst.setString(6, baTxtFld.getText()); 
     pst.setString(7, caTxtFld.getText()); 
     pst.setString(8, paTxtFld.getText()); 
     pst.setString(9, cntryTxtFld.getText()); 
     pst.setString(10, String.valueOf(monthCBox.getSelectedItem())); 
     pst.setString(11, String.valueOf(dayCBox.getSelectedItem())); 
     pst.setString(12, String.valueOf(yearCBox.getSelectedItem())); 
     pst.setString(13, getSelectedButtonText(bg)); 
     pst.execute(); 

     JOptionPane.showMessageDialog(null, "Entry Saved!"); 

    } catch (SQLException | HeadlessException e) { 
     JOptionPane.showMessageDialog(null, e); 
    } 

} 

回答

1

將單引號轉義爲Driver's ID。類似的,

String sql = "INSERT INTO dr_record_table (Driver''s ID, Last Name, " 
    + "First Name, Middle Name, Street, Barangay, City/Municipality, " 
    + "Province, Country, Month, Day, Year, Gender) VALUES " 
    + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
+0

謝謝先生!我終於修好了這個! :) –

相關問題