gpt4 book ai didi

java.sql.SQLException : Parameter index out of range (2 > number of parameters, 为 0)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:03 28 4
gpt4 key购买 nike

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: 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);

关于java.sql.SQLException : Parameter index out of range (2 > number of parameters, 为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com