简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM