java.sql.SQLException:参数索引超出范围(2>参数个数,为0)
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(); }
当我尝试使用上面的代码插入数据时,它抛出一个exception:
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)
我试过看一下但是我找不到问题请帮忙!
您将在位置2处开始插入而不是1
pstmt.setString(2,txtFirtsName.getText());
将其更改为
pstmt.setString(1,txtFirtsName.getText());
为最后一个人做同样的事情
pstmt.setBytes(9, person_image);
您的SQL语句中只有9个参数。
String Sql="INSERT INTO brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) VALUES(?,?,?,?,?,?,?,?,?)";
当您注释掉第一个setString
方法调用时,您应该重新编号以下调用的参数索引。 它们应该编号为1 – 9,而不是2 – 10。
问号的数量?
必须等于参数。
如果您有7个参数,那么您的语句中应该有7个问号,即insert into table name values(?,?,?,?,?,?,?)
第一个参数的索引应该是1而不是2,最后一个参数的索引应该是9而不是10.当分配N个参数时,索引从1到N.
使用psmt.executeUpdate(); 而不是pstmt.executeUpdate(Sql);
- 声明方法是否会抛出未经检查的exception是否有优势?
- java.net.URISyntaxException
- javaexception会终止整个java应用程序吗?
- Java大十进制数格式exception
- 你能弄清楚为什么这个程序会触发IllegalStateException吗?
- 获取exceptionorg.apache.logging.slf4j.SLF4JLoggerContext无法强制转换为org.apache.logging.log4j.core.LoggerContext
- 在struts xml中声明/处理哪些exception
- PSQLException:此ResultSet已关闭
- java.io.IOException的可能原因是什么:“文件名,目录名称或卷标语法不正确”