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)
我試圖查看它,但我不能找到問題,請幫助!
當我用這個代替我能除了畫面中插入數據: 嘗試{ 字符串addrecords =「插入兄弟(名字,中間名,姓,出生日期,出生地,國籍,InstituteNumber,狀態,圖片)values +'''+ fname +「','」+ mname +「','」+ lname +「','」+ dave +「','」+ place +「','」 + isa +「','」+ InstNo +「','」+ statusq +「','」+ photo +「')」; // wrapField(); st.executeUpdate(addrecords); } – Isaiah 2013-04-24 15:56:42
您應該更改問題的標題以匹配您在問題正文中放置的異常的實際消息! – 2013-04-25 07:00:08