[英]Insert values to mysql Java
It gives this error: 它给出了这个错误:
" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). "
“ java.sql.SQLException:参数索引超出范围(1>参数数量,为0)。 ”
This is the code: 这是代码:
public void insertData(int id,String name, String status, String age){
String sql = "INSERT INTO student_table (id,name,satus,age) VALUES (id,name,status,age)";
try (Connection conn1 = this.connects();PreparedStatement Prepst= conn1.prepareStatement(sql)){
Prepst.setInt(1, id);
Prepst.setString(2, name);
Prepst.setString(3, status);
Prepst.setString(4, age);
Prepst.executeUpdate();
}catch(Exception e){
System.err.println(e);
}
If I change the parameter index to another value ie: 如果我将参数索引更改为另一个值,即:
Prepst.setInt(0, id);
then it gives this error: 那么它给出了这个错误:
" java.sql.SQLException: Parameter index out of range (0 < 1 ). "
“ java.sql.SQLException:参数索引超出范围(0 <1)。”
----UPDATE ----- ----更新-----
I inserted this code: 我插入了以下代码:
String sql = "INSERT INTO student_table (id,name,satus,age) VALUES (?,?,?,?)";
It produces the same: 它产生相同的结果:
Table Structure: 表结构:
Your SQL expression 您的SQL表达式
INSERT INTO student_table (id,name,satus,age) VALUES (id,name,status,age)
actually does not have any parameters. 实际上没有任何参数。 Try with:
尝试:
INSERT INTO student_table (id,name,satus,age) VALUES (?,?,?,?)
Try this... 尝试这个...
public void insertData(int id,String name, String status, String age){
String sql = "INSERT INTO student_table (id,name,satus,age) VALUES (?,?,?,?)";
try (Connection conn1 = this.connection;PreparedStatement Prepst= conn1.prepareStatement(sql)){
Prepst.setInt(1, id);
Prepst.setString(2, name);
Prepst.setString(3, status);
Prepst.setString(4, age);
Prepst.executeUpdate();
}catch(Exception e){
System.err.println(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.