我正在像这样将参数传递给 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);
}
}
查询看起来像这样:
select * from table_1 where run_id = ?
现在我想像这样修改我的查询,并重用第一个参数(?
都将使用 runId 参数):
select * from table_1 where run_id = ?
union
select * from table_2 where run_id = ?
不这样做是否可能:
ps.setString(1, runId);
ps.setString(2, runId);
最佳答案
这不能用普通的 JDBC 来完成。您可以改为使用 Spring 的 JDBCTemplate,它将支持您想要的命名参数,并在语句中需要的任何地方重复使用相同的名称。
关于java - 在 PreparedStatement 中重用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501434/