Is there a way to use the same variable numerous times in a prepared statement with the jdbc postgresql driver?
For example I want to use the following statement but have the same value for each question mark:
PreparedStatement ps =
pg.prepareCall("SELECT * FROM mytable WHERE col1=? AND col2=? AND col3=?");
but instead of entering the code three times, such as:
ps.setInt(0,1);
ps.setInt(1,1);
ps.setInt(2,1);
is there a way to simply use the same value in each of the variables? I’m think of something similar to the usage in pypostgresql where one can simply say $1 for use with each of the fields.
"SELECT * FROM mytable WHERE col1=$1 AND col2=$1 AND col3=$1"
To the best of my knowledge, plain JDBC does not support named query parameters. You might make a small function to put the value in all of the places. Otherwise you are looking at using something heavier like Spring or JPA.