将相同参数添加到准备好的语句中的多个位置的巧妙方法

发布于 2024-09-09 10:27:23 字数 308 浏览 6 评论 0原文

我有一个 JDBC 查询,就像

select * from table1 where col1 between x and y
union all
select * from table2 where col1 between x and y
union all
select * from table3 where col1 between x and y

我正在使用准备好的语句一样,我想知道是否有一种更聪明的方法来设置 x 和 y 而无需说 setDate(1, x);setDate(2, y);setDate(3 , x);

I have a JDBC query like

select * from table1 where col1 between x and y
union all
select * from table2 where col1 between x and y
union all
select * from table3 where col1 between x and y

I'm using a prepared-statement and am wondering if there is a cleverer way to set x and y without saying setDate(1, x);setDate(2, y);setDate(3, x);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寒冷纷飞旳雪 2024-09-16 10:27:23

更聪明。也许是一个循环?执行 3 次并调用的 for 循环:

setDate((1*2)+1, x);
setDate((1*2)+2, y);

Cleverer. Maybe a loop? A for loop that executes three times and calls:

setDate((1*2)+1, x);
setDate((1*2)+2, y);
如此安好 2024-09-16 10:27:23

如果您的数据库不错,您可以这样做:

select * from (
select * from table1 
union all
select * from table2
union all
select * from table3
)
where col1 between x and y

并仅传递 xy 一次。如果合适的话,优化器会将谓词应用于每个表。

If your DB is decent, you can do:

select * from (
select * from table1 
union all
select * from table2
union all
select * from table3
)
where col1 between x and y

and pass x and y just once. The optimizer would apply the predicate to each table if appropiate.

情愿 2024-09-16 10:27:23

为什么要“聪明”?聪明的代码通常会在“聪明”的极端情况下导致“聪明”的错误。

顺便说一句,如果您使用 hibernate,您的查询将是:

select * from table1 where col1 between :x and :y
union all
select * from table2 where col1 between :x and :y
union all
select * from table3 where col1 between :x and :y

Java 代码将如下所示:

Query query;
query.setParameter("x", x);
query.setParameter("y", y);

why be "clever"? Clever code usually results in "clever" bugs in "clever" corner cases.

BTW if you use hibernate, your query would be:

select * from table1 where col1 between :x and :y
union all
select * from table2 where col1 between :x and :y
union all
select * from table3 where col1 between :x and :y

Java code would look like this:

Query query;
query.setParameter("x", x);
query.setParameter("y", y);
2024-09-16 10:27:23

我会利用java的字符串格式化程序:

String queryFormat = "select * from table1 where col1 between %1$s and %2$s " +
                     "union all " +
                     "select * from table2 where col1 between %1$s and %2$s " +
                     "union all " +
                     "select * from table3 where col1 between %1$s and %2$s";
String query = String.format(queryFormat,"5","10");

传递给格式方法的第一个参数是格式字符串。 %1$s 表示插入 string 类型的第 1 个参数(“5”),以及 %2 $s 表示插入string 类型的第二参数(“10”)。

然后,查询字符串将包含:

select * from table1 where col1 between 5 and 10 union all select * from table2 where col1 between 5 and 10 union all select * from table3 where col1 between 5 and 10

您可以阅读有关 Formatter 类的更多信息 此处

希望这有帮助。

I would take advantage of java's String Formatter:

String queryFormat = "select * from table1 where col1 between %1$s and %2$s " +
                     "union all " +
                     "select * from table2 where col1 between %1$s and %2$s " +
                     "union all " +
                     "select * from table3 where col1 between %1$s and %2$s";
String query = String.format(queryFormat,"5","10");

The first argument passed to the format method is the format string. The %1$s means to insert the 1st argument of type string ("5"), and the %2$s means to insert the 2nd argument of type string ("10").

The query string will then contain:

select * from table1 where col1 between 5 and 10 union all select * from table2 where col1 between 5 and 10 union all select * from table3 where col1 between 5 and 10

You can read more about the Formatter class here.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文