Reputation: 11
It gives this error:
"java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 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 i.e.:
Prepst.setInt(0, id);
then it gives this error:
" java.sql.SQLException: Parameter index out of range (0 < 1 ). "
----UPDATE -----
I inserted this code:
String sql = "INSERT INTO student_table (id,name,satus,age) VALUES (?,?,?,?)";
It produces the same:
Table Structure:
Upvotes: 0
Views: 92
Reputation: 469
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);
}
}
Upvotes: 0
Reputation: 43709
Your SQL expression
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 (?,?,?,?)
Upvotes: 3