在PreparedStatement中重用参数?
16lz
2021-01-22
I am passing a parameter to a PreparedStatement like this :
我将参数传递给PreparedStatement,如下所示:
public void getNodes(String runId, File file, Connection conn) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(Mat.queries.get("NettingNode.QUERY"));
ps.setString(1, runId);
ps.setFetchSize(10000);
rs = ps.executeQuery();
// etc.
} catch (Exception e) {
logger.error(e, e);
} finally {
close(rs, ps);
}
}
And the query looks like this :
查询看起来像这样:
select * from table_1 where run_id = ?
Now I want to modify my query like this, and reuse the first parameter (both ?
would use the runId parameter) :
现在我想像这样修改我的查询,并重用第一个参数(两个?将使用runId参数):
select * from table_1 where run_id = ?
union
select * from table_2 where run_id = ?
Is that possible without doing this :
没有这样做可能是这样的:
ps.setString(1, runId);
ps.setString(2, runId);
1 个解决方案
#1
9
This cannot be done with plain JDBC. You could instead use Spring's JDBCTemplate, which would support what you want with Named Parameters, and re-use the same name wherever it's needed in the statement.
使用普通JDBC无法做到这一点。您可以改为使用Spring的JDBCTemplate,它可以支持命名参数所需的内容,并在语句中的任何位置重用相同的名称。
See Named parameters in JDBC
请参阅JDBC中的命名参数
更多相关文章
- LINUX下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结
- C#中操作Oracle时的SQL语句参数的用法
- 彻底理解初始化参数SERVICE_NAMES和客户端TNS中的SERVICE_NAME
- sql 存储过程参数为空则不作为条件
- mysql参数优化辅助工具之tuning-primer.sh
- MSSQL中类似MySQL的limit参数
- 求助,关于sql带入参数的写法问题。
- 参数化的Insert语句,事务抛出错误
- 即使提供了参数,过程也需要参数