mysql - java.sql.SQLException Parameter index out of range (1 > number of parameters, which is 0) -
after validation of select combo box have selected not able insert in database. tomcat gives following error
java.sql.sqlexception: parameter index out of range (1 > number of parameters, 0).
how caused , how can solve it?
you error when call of setxxx()
methods on preparedstatement
, while sql query string not have placeholders ?
this.
for example wrong:
string sql = "insert 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 fix sql query string accordingly specify placeholders.
string sql = "insert tablename (col1, col2, col3) values (?, ?, ?)"; // ... preparedstatement = connection.preparestatement(sql); preparedstatement.setstring(1, val1); preparedstatement.setstring(2, val2); preparedstatement.setstring(3, val3);
note parameter index starts 1
, not need quote placeholders so:
string sql = "insert tablename (col1, col2, col3) values ('?', '?', '?')";
otherwise still same exception, because sql parser interpret them actual string values , can't find placeholders anymore.
Comments
Post a Comment