I am passing a parameter to a PreparedStatement like this :
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) :
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);
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.