Sayan
Sayan

Reputation: 57

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

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

Upvotes: 3

Views: 1756

Answers (2)

Maciej
Maciej

Reputation: 2004

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

Upvotes: 3

wafaa hegazy
wafaa hegazy

Reputation: 97

try to specify colum names you want to insert

    String query = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
     preparedStatement = connection.prepareStatement(query);

Upvotes: 0

Related Questions