2013-04-24 44 views
3
public class Brothers extends javax.swing.JFrame { 

    /** 
    * declaring connection and SQL statement 
    */ 

    Connection cn; 
    Statement st; 
    PreparedStatement pstmt=null; 
    PreparedStatement pst; 
    ResultSet rs; 
    Object fname, mname, lname, bdate, nation, statusq,InstNo, photo, combo, place, mimi; 
    int status; 



    public Brothers() {   
     dbconnect _db = new dbconnect(); 

/*//////////////the above is just to show that I have two prepared statement each for each sql statement that i have //////////*/ 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      //String unicode= "?useUnicode=yes&characterEncoding=UTF-8"; 
      cn = DriverManager.getConnection(_db.getHost(), _db.getUsername(), _db.getPassword()); 
      st=cn.createStatement(); 

try{ 

     String Sql="INSERT INTO brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) VALUES(?,?,?,?,?,?,?,?,?)"; 
     pstmt=cn.prepareStatement(Sql); 
     //pstmt.setString(1,txtTrial.getText());('?','?','?','?','?','?','?','?','?') 
     pstmt.setString(2,txtFirtsName.getText()); 
     pstmt.setString(3,txtMiddleName.getText()); 
     pstmt.setString(4,txtLastName.getText()); 
     pstmt.setString(5,((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText()); 
     pstmt.setString(6,txtPlacBirth.getText()); 

     String nations=combonation.getSelectedItem().toString(); 
     pstmt.setString(7,nations); 
     pstmt.setString(8,txtInstituteNo.getText()); 


     pstmt.setObject(9,combostatus.getSelectedItem()); 
     pstmt.setBytes(10, person_image); 

     pstmt.executeUpdate(Sql); 

     JOptionPane.showMessageDialog(null, "Saving Successfull"); 

     } 

     catch(Exception e){ 

      //JOptionPane.showMessageDialog(null, e); 
      e.printStackTrace(); 
    } 

當我使用上述代碼嘗試插圖數據拋出異常:java.sql.SQLException中:參數索引超出範圍(2>的參數號,它是0)

java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9). 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) 
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813) 
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795) 

我試圖查看它,但我不能找到問題,請幫助!

+0

當我用這個代替我能除了畫面中插入數據: 嘗試{ 字符串addrecords =「插入兄弟(名字,中間名,姓,出生日期,出生地,國籍,InstituteNumber,狀態,圖片)values +'''+ fname +「','」+ mname +「','」+ lname +「','」+ dave +「','」+ place +「','」 + isa +「','」+ InstNo +「','」+ statusq +「','」+ photo +「')」; // wrapField(); st.executeUpdate(addrecords); } – Isaiah 2013-04-24 15:56:42

+0

您應該更改問題的標題以匹配您在問題正文中放置的異常的實際消息! – 2013-04-25 07:00:08

回答

2

您的SQL語句中只有9個參數。

String Sql="INSERT INTO brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) VALUES(?,?,?,?,?,?,?,?,?)"; 

當你註釋掉第一setString方法調用,你應該重新編號的參數指標對下列呼叫。它們應該編號爲1 - 9,而不是2 - 10.

+0

我按照您所建議的錯誤更改爲: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您在SQL語法中有錯誤;請查看第1行 \t at sun.reflect.NativeConstructorAccessorImpl.newInstance0附近的「?,?,?,?,?,?,?,?,?」附近使用的正確語法對應的MySQL服務器版本手冊(Native Method) – Isaiah 2013-04-25 07:03:51

+0

我的表有十列:BrothersID,FirstName,MiddleName,LastName,BirthDate,BirthPlace,國籍,InstituteNumber,狀態,圖片。 BrothersID是一個自動增量,這意味着我不必輸入任何內容!但我甚至試圖通過創建一個我不使用的文本字段來欺騙它...看到//pstmt.setString(1,txtTrial.getText());但它仍然沒有工作 – Isaiah 2013-04-25 08:36:55

0

第一個參數的索引應該是1而不是2,最後一個參數的索引應該是9而不是10.當分配N個參數時,索引從1到N

+0

當我按照你所建議的錯誤更改爲:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL語法中有錯誤;在第1行sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native())檢查與您的MySQL服務器版本相對應的手冊,以找到在'?,?,?,?,?,?,?,?,?方法) – Isaiah 2013-04-25 07:05:07

6

您所在的位置2日開始插入,而不是1

pstmt.setString(2,txtFirtsName.getText()); 

將其更改爲

pstmt.setString(1,txtFirtsName.getText()); 

做別人相同,其中最後一個將是

pstmt.setBytes(9, person_image); 
+0

當然你是對的,但是要麼有另一個問題,要麼是錯誤消息「參數索引超出範圍(2>參數數量,這是0)」是相當具有誤導性的。據我所知,你不必按順序設置參數(你可以設置2,然後是3,然後是1),所以當他試圖設置10而不是2時,它應該會崩潰? – 2013-04-24 16:21:05

+0

請再讀一遍例外,它是「(10>參數數量,即9)」,這意味着他試圖在位置10插入一個參數,但它只支持9個參數。我也沒有說他需要按順序設置參數。 – fmodos 2013-04-24 16:55:40

+0

我的表格有十列:BrothersID,FirstName,MiddleName,LastName,BirthDate,BirthPlace,國籍,InstituteNumber,狀態,圖片。 BrothersID是一個自動增量,這意味着我不必輸入任何內容!但我甚至試圖通過創建一個我不使用的文本字段來欺騙它...看到//pstmt.setString(1,txtTrial.getText());但它仍然無效 – Isaiah 2013-04-25 06:57:32

2

問號?的數量必須等於該參數。

如果你有7個參數,那麼就應該在你的發言7問號即insert into table name values(?,?,?,?,?,?,?)

0

使用 psmt.executeUpdate();代替 pstmt.executeUpdate(Sql);

相關問題