Skip to content
Advertisement

how to set concatenated string parameter in preparedstatement

I have the following java code:

1- String sql="select * from mytable where params='file_name=?,report_name=?'"
2- stmt.setString(1, fileName);
3- stmt.setString(2, reportName);

I get the following exception in codeline 3-:

Invalid column index

What did I do wrong? how to set params correctly?

Advertisement

Answer

Take the bind parameters out of the string literal:

String sql="select * from mytable where params='file_name=' || ? || ',report_name=' || ?"

Or, create a single bind parameter and pass in the concatenated value:

String sql="select * from mytable where params=?"
stmt.setString(1, "file_name=" + fileName + ",report_name=" + reportName);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement