java.sql.SQLException:参数索引超出范围(2>参数个数,为0) [英] java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 0)

查看:209
本文介绍了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(); 
    }  

当我尝试使用上面的代码插入数据时会引发异常:

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!!

推荐答案

你正在开始而是插入位置2而不是1

You are starting the insert at position 2 instead 1

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

将其更改为

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

对于最后一个将为
$的其他人做同样的事情b $ b

Do the same for the others where the last one will be

pstmt.setBytes(9, person_image);

这篇关于java.sql.SQLException:参数索引超出范围(2>参数个数,为0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆