简体   繁体   English

如何在preparedstatement中设置连接字符串参数

[英]how to set concatenated string parameter in preparedstatement

I have the following java code:我有以下 java 代码:

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-:我在代码行 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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