简体   繁体   English

参数索引超出范围(8>参数数量,即7)

[英]Parameter index out of range (8 > number of parameters, which is 7)

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). 运行此代码时,出现错误:参数索引超出范围(8>参数数量,即7)。 I also tried changing setString(1,fn) but it's not working. 我也尝试更改setString(1,fn)但是它不起作用。

 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();
 }

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

You are passing 8 columns and 7 variables, count doesn't match. 您正在传递8列和7个变量,计数不匹配。

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 (?,?,?,?,?,?,?)";

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

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. 注意: ++c是预增量运算符。 It adds 1 to the current value of c (sets c to this) and uses the new value of c . 它将c的当前值加c (为此设置c )并使用c的新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数索引超出范围(4 > 参数个数,即 3) - Parameter index out of range (4 > number of parameters, which is 3) 参数索引超出范围(4 > 参数个数,即 2) - Parameter index out of range (4 > number of parameters, which is 2) 参数索引超出范围(3>参数数量,即2) - Parameter index out of range (3 > number of parameters, which is 2) 参数索引超出范围(1>参数数量,即0) - Parameter index out of range (1 > number of parameters, which is 0) Java参数索引超出范围(2>参数数量,即1) - Java Parameter Index Out Of Range (2 > number of parameters, which is 1) Java 代码中的错误:参数索引超出范围(1 > 参数数量,为 0) - Error in Java code :Parameter index out of range (1 > number of parameters, which is 0) 参数索引超出范围(1>参数数量,即0)。 怎么解决呢? - Parameter index out of range (1 > number of parameters, which is 0). how to solve it? 参数索引超出范围(1>参数数量,即0),如何避免? - Parameter index out of range (1 > number of parameters, which is 0), how to avoid? 如何解决:“参数索引超出范围(2 > 参数数量,即 1)。” - How to fix: “Parameter index out of range (2 > number of parameters, which is 1).” 参数索引超出范围(1 > 参数数量,即 0)。 Mysql 与 Java - Parameter index out of range (1 > number of parameters, which is 0). Mysql vs Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM