简体   繁体   中英

Parameter index out of range (3 > number of parameters, which is 2)

    public static int updateApproved(admin u){  
        int status=0;  
        try{  
            Connection con=getConnection();  
            PreparedStatement ps=con.prepareStatement("update admission set status=? where admiss_id=?");  

            ps.setString(1,u.getStatus());
            ps.setInt(2,u.getAdmiss_id());  


            status=ps.executeUpdate();

            PreparedStatement ps2=con.prepareStatement("insert into patient(username,password,email,sex,level,fullname,age,bday,blood,address,vaccines,fam_his,surgery,medicine_taken) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");  
            ps.setString(1,u.getUsername());  
            ps.setString(2,u.getPassword());  
            ps.setString(3,u.getEmail());  
            ps.setString(4,u.getSex());  
            ps.setInt(5,u.getLevel());
            ps.setString(6,u.getFullname());
            ps.setInt(7,u.getAge());
            ps.setString(8,u.getBday());
            ps.setString(9,u.getAddress());
            ps.setString(10,u.getBlood());
            ps.setString(11,u.getVaccines());
            ps.setString(12,u.getFam_his());
            ps.setString(13,u.getSurgery());
            ps.setString(14,u.getMedicine_taken());
            status=ps2.executeUpdate();
        }catch(Exception e){System.out.println(e);}  
        return status;  
    }  

java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). Why is this always the error? i have counter the ranges of the parameter, but i still get that error.

PreparedStatement ps2=con.prepareStatement("insert into patient(username,password,email,sex,level,fullname,age,bday,blood,address,vaccines,fam_his,surgery,medicine_taken) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");  
ps.setString(1,u.getUsername());  // ps has this
ps.setString(2,u.getPassword());  // ps has this
ps.setString(3,u.getEmail());  // ps does not have this, it only has 2 ?'s in it, so it explodes

You're making PreparedStatement ps2 but your setString s are all on ps ... You need to update those to use ps2

The problem is you have misused the variables ps and ps2 .

As you have created a PreparedStatement variable above, you can use it again without creating a new one.

ps = con.prepareStatement("insert into patient
(username, password, email, sex,
level, fullname, age, bday, blood, address, vaccines, fam_his, surgery, medicine_taken)
values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM