46

After validation of select combo box which I have selected and I am not able to insert it in my database. Tomcat gives following error:

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

How is this caused and how can I solve it?

6
  • 1
    Can you post your jsp code with sql statement and error stacktrace ? Commented Jun 5, 2012 at 11:08
  • 4
    It means that you're trying to set a parameter on a query which doesn't contain any parameter. The stack trace tells you exactly at which line of which file the exception is thrown. Also, you should accept answers to your previous questions. Commented Jun 5, 2012 at 11:09
  • I am not able to solve this error..so not accepted Commented Jun 5, 2012 at 11:28
  • @yatin: JB Nizet was referring to your previous questions. Commented Jun 5, 2012 at 11:30
  • yeah previous answers i accepted and I said that Commented Jun 5, 2012 at 11:42

2 Answers 2

89

You will get this error when you call any of the setXxx() methods on PreparedStatement, while the SQL query string does not have any placeholders ? for this.

For example this is wrong:

String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (val1, val2, val3)";
// ...

preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, val1); // Fail.
preparedStatement.setString(2, val2);
preparedStatement.setString(3, val3);

You need to fix the SQL query string accordingly to specify the placeholders.

String sql = "INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?)";
// ...

preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, val1);
preparedStatement.setString(2, val2);
preparedStatement.setString(3, val3);

Note the parameter index starts with 1 and that you do not need to quote those placeholders like so:

String sql = "INSERT INTO tablename (col1, col2, col3) VALUES ('?', '?', '?')";

Otherwise you will still get the same exception, because the SQL parser will then interpret them as the actual string values and thus can't find the placeholders anymore.

See also:

Sign up to request clarification or add additional context in comments.

3 Comments

In my case this occurred because I used JDBCTemplate instead of NamedParameterJDBCTemplate
I get the same error INSERT INTO Bslash2 (`end\`) values(?) due to one of my column names ends with backslash ?? i don't know why
How can we define list ?
14

This is an issue with the jdbc Driver version. I had this issue when I was using mysql-connector-java-commercial-5.0.3-bin.jar but when I changed to a later driver version mysql-connector-java-5.1.22.jar, the issue was fixed.

3 Comments

similar issue observed with mysql-connector-java-5.0.8-bin.jar
Thank you, even on spring boot 2.3.0 i encountered this, upgraded to 2.3.1, and it worked flawesly.
strange! I got this same error just because of a small typo like missing ` (altgr+7 in azerty keyboard) ex: "SELECT sh`.shift_count FROM ..." , I spent many hours to figure it out, so just in case...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.