参数指标超出范围(8个>参数个数,为7)

时间:2022-12-31 16:40:18

I have this database table:

我有这个数据库表:

create table users(
    id int not null auto_increment,
    fn varchar(30),
    ln varchar(30),
    sex char,
    email varchar(60),
    country varchar(40),
    username varchar(30),
    password varchar(100),
    primary key(id)
);

When I run this code, I am getting an error: Parameter index out of range (8 > number of parameters, which is 7). I also tried changing setString(1,fn) but it's not working.

当我运行这个代码时,我得到了一个错误:参数索引超出了范围(8个>的参数,也就是7个)。

 try{
     String INSERT="INSERT INTO users (fn,ln,,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";
     PreparedStatement pst=conn.prepareStatement(INSERT);
     System.out.println("Created prepared statement");

     pst.setString(2,"fn");
     pst.setString(3,"ln");
     pst.setString(4,"sex");
     pst.setString(5,"email");
     pst.setString(6,"country");
     pst.setString(7,"username");
     pst.setString(8,"password)");
     pst.executeUpdate();
 }

4 个解决方案

#1


4  

you have an extra comma in your query and your column count should start from 1.

在查询中有一个额外的逗号,列计数应该从1开始。

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";
pst.setString(1,"fn");
    pst.setString(2,"ln");
    pst.setString(3,"sex");
    pst.setString(4,"email");
    pst.setString(5,"country");
    pst.setString(6,"username");
    pst.setString(7,"password)");
    pst.executeUpdate();

#2


0  

You are passing 8 columns and 7 variables, count doesn't match.

你传递了8个列和7个变量,count不匹配。

Make sure if this:

确保如果这个:

String INSERT="INSERT INTO users (fn,ln,,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

should be like this:

应该是这样的:

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

#3


0  

Parameter number relative to the query

参数编号相对于查询。

pst.setString(1,"fn");
pst.setString(2,"ln");
pst.setString(3,"sex");
pst.setString(4,"email");
pst.setString(5,"country");
pst.setString(6,"username");
pst.setString(7,"password)");
pst.executeUpdate();

#4


0  

Corrections:

修正:

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

and

short c = 0;
    //using a counter variable (short or int) means
    //no worries about numbering - just maintain the order below
pst.setString(++c,"fn");
pst.setString(++c,"ln");
pst.setString(++c,"sex");
pst.setString(++c,"email");
pst.setString(++c,"country");
pst.setString(++c,"username");
pst.setString(++c,"password)");

Note: ++c is pre-increment operator. It adds 1 to the current value of c (sets c to this) and uses the new value of c.

注意:++c是预增量运算符。它向c的当前值加1,并使用c的新值。

#1


4  

you have an extra comma in your query and your column count should start from 1.

在查询中有一个额外的逗号,列计数应该从1开始。

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";
pst.setString(1,"fn");
    pst.setString(2,"ln");
    pst.setString(3,"sex");
    pst.setString(4,"email");
    pst.setString(5,"country");
    pst.setString(6,"username");
    pst.setString(7,"password)");
    pst.executeUpdate();

#2


0  

You are passing 8 columns and 7 variables, count doesn't match.

你传递了8个列和7个变量,count不匹配。

Make sure if this:

确保如果这个:

String INSERT="INSERT INTO users (fn,ln,,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

should be like this:

应该是这样的:

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

#3


0  

Parameter number relative to the query

参数编号相对于查询。

pst.setString(1,"fn");
pst.setString(2,"ln");
pst.setString(3,"sex");
pst.setString(4,"email");
pst.setString(5,"country");
pst.setString(6,"username");
pst.setString(7,"password)");
pst.executeUpdate();

#4


0  

Corrections:

修正:

String INSERT="INSERT INTO users (fn,ln,sex,email,country,username,password) VALUES (?,?,?,?,?,?,?)";

and

short c = 0;
    //using a counter variable (short or int) means
    //no worries about numbering - just maintain the order below
pst.setString(++c,"fn");
pst.setString(++c,"ln");
pst.setString(++c,"sex");
pst.setString(++c,"email");
pst.setString(++c,"country");
pst.setString(++c,"username");
pst.setString(++c,"password)");

Note: ++c is pre-increment operator. It adds 1 to the current value of c (sets c to this) and uses the new value of c.

注意:++c是预增量运算符。它向c的当前值加1,并使用c的新值。