Reuse a parameter in a PreparedStatement?

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


Source: sql

Leave a Reply