我有这样的PreparedStatement:
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)");
每个子查询的参数值都是相同的。如何仅为一个子查询设置参数并自动填充另一个子查询的参数(不是每个子查询)?
答案 0 :(得分:1)
您可以使用命名参数,这样当参数在查询中多次出现时,您只需指定一次。 JDBC不支持命名参数,但您可以编写代码以在SQL字符串中查找参数名称,并确定它们出现的顺序,以便可以将参数添加到PreparedStatement。 Spring-Jdbc使用NamedParameterJdbcTemplate here's an example为您完成此操作。这是核心弹簧,实现参数重组的包是org.springframework.jdbc.core.namedparam
。