如何在JDBC Postgresql预准备语句中多次使用相同的值 [英] How to use the same value multiple times in jdbc postgresql prepared statement

查看:42
本文介绍了如何在JDBC Postgresql预准备语句中多次使用相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过jdbc postgresql驱动程序在准备好的语句中多次使用相同的变量?

例如,我想使用以下语句,但是每个问号的值都相同:

  PreparedStatement ps =pg.prepareCall("SELECT * FROM mytable WHERE col1 =?AND col2 =?AND col3 =?"); 

,而不是输入三次代码,例如:

  ps.setInt(0,1);ps.setInt(1,1);ps.setInt(2,1); 

有没有一种方法可以在每个变量中简单地使用相同的值?我认为与 pypostgresql 可以简单地说出 $ 1 用于每个字段.

 "SELECT * FROM mytable WHERE col1 = $ 1 AND col2 = $ 1 AND col3 = $ 1" 

就我所知,纯JDBC不支持命名查询参数.您可能会做一个小函数,将值放置在所有位置.否则,您会考虑使用诸如Spring或JPA之类的更重的东西.

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.

这篇关于如何在JDBC Postgresql预准备语句中多次使用相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆