java.sql.SQLException: 参数索引超出范围(2 > 参数数量,为 0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16196670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 0)
提问by Isaiah
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();
}
When I try to inset data using the above code it throws an 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)
I have tried to look over it but I cant find the problem please help!!
我试图查看它,但我找不到问题请帮忙!!
回答by Bill the Lizard
You only have 9 parameters in your SQL statement.
您的 SQL 语句中只有 9 个参数。
String Sql="INSERT INTO brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) VALUES(?,?,?,?,?,?,?,?,?)";
When you commented out the first setString
method call, you should have renumbered the parameter indices for the following calls. They should be numbered 1 - 9, not 2 - 10.
当您注释掉第一个setString
方法调用时,您应该为以下调用重新编号参数索引。它们应该编号为 1 - 9,而不是 2 - 10。
回答by Toni Toni Chopper
The index of the first parameter should be 1 not 2, the index of the last parameter should be 9 not 10. When assigning N parameters, the index goes from 1 to N.
第一个参数的索引应该是1而不是2,最后一个参数的索引应该是9而不是10。当分配N个参数时,索引从1到N。
回答by fmodos
You are starting the insert at position 2 instead 1
您在位置 2 而不是 1 处开始插入
pstmt.setString(2,txtFirtsName.getText());
Change it to
将其更改为
pstmt.setString(1,txtFirtsName.getText());
Do the same for the others where the last one will be
对其他人做同样的事情,最后一个将在那里
pstmt.setBytes(9, person_image);
回答by user3235752
The number of question marks ?
must equal to the parameter.
问号的数量?
必须等于参数。
If you have 7 parameter then there should be 7 question marks in your statement i.e insert into table name values(?,?,?,?,?,?,?)
如果您有 7 个参数,那么您的语句中应该有 7 个问号,即 insert into table name values(?,?,?,?,?,?,?)
回答by rao arsalan
use psmt.executeUpdate(); instead of pstmt.executeUpdate(Sql);
使用 psmt.executeUpdate(); 而不是 pstmt.executeUpdate(Sql);