Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0-

Tags:

java

mysql

jdbc

String productName = request.getParameter("productName");
int productPrice = Integer.parseInt(request.getParameter("productPrice"));

String query = " INSERT INTO PRODUCTS values(?,?,?)";
PreparedStatement pst = (PreparedStatement) con.prepareStatement("query");
int i = 1;

pst.setInt(1, i);
pst.setString(2, productName);
pst.setInt(3, productPrice);
i++;
pst.executeUpdate(query);
con.close();
like image 253
Sayan Avatar asked Nov 23 '25 19:11

Sayan


1 Answers

change

PreparedStatement pst = (PreparedStatement) con.prepareStatement("query");

to

PreparedStatement pst = (PreparedStatement) con.prepareStatement(query);

Notice that you need to pass actual query variable not a "query" string

and as Mark noted below: In addition pst.executeUpdate(query) needs to be changed to pst.executeUpdate().

like image 117
Maciej Avatar answered Nov 26 '25 09:11

Maciej