Set parameter for identical subquery in PreparedStatement

I have a PreparedStatement like this:

PreparedStatement st = conn
                    .prepareStatement("Select * from "
                            + "(select Count(*) from uch_otstyp b,prov_uch p "
                            + "where b.\"ID_Poezd\"=?"
                            + "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
                            + "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
                            + "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
                            + "and b.\"ID_Uch\"=p.\"ID_Uch\" "
                            + "and p.\"MES\"=? "
                            + "and p.\"GOD\"=? "
                            + "and p.\"Nput\"=? "
                            + "and b.\"Kod_Otstup\"=? "
                            + "and b.\"DEPTH\"<1),"
                            + ""
                            + "(select Count(*) from uch_otstyp b,prov_uch p "
                            + "where b.\"ID_Poezd\"=?"
                            + "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
                            + "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
                            + "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
                            + "and b.\"ID_Uch\"=p.\"ID_Uch\" "
                            + "and p.\"MES\"=? "
                            + "and p.\"GOD\"=? "
                            + "and p.\"Nput\"=? "
                            + "and b.\"Kod_Otstup\"=? "
                            + "and b.\"DEPTH\">=1)"
                            + "and b.\"DEPTH\"<2)");

      

The parameter values ​​for each subquery are identical. How can I set parameters for just one subquery and auto-populate the parameters for another (not for every subquery)?

+3


source to share


1 answer


You can use named parameters, so when a parameter appears multiple times in a request, you only need to specify it once. JDBC does not support named parameters, but you can write code to find the parameter names in the SQL string and figure out the order in which they appear so that the arguments can be added to the PreparedStatement. Spring -Jdbc does this for you using NamedParameterJdbcTemplate, here is an example . This is the core spring, a package that implements the parameter munging-, org.springframework.jdbc.core.namedparam

.



+1


source







All Articles