如何使用数组列表作为准备好的语句参数 [英] How to use an arraylist as a prepared statement parameter
问题描述
我查看了以下挑战,但一直无法找到答案.它看起来很简单,但我一直无法解决.
I have looked and have been unable to find an answer to the following challenge I am having. It seems pretty straightforward but I have been unable to resolve it.
我有一个 ArrayList
的记录 ID,类型为 Long
-> ArrayList
.我想用此记录 ID 列表用于从另一个表中选择行.到现在为止还挺好.现在到挑战...
I have an ArrayList
of record ids that are type Long
-> ArrayList<Long>
. I would like to use
this list of record ids to select rows from another table. So far so good. Now onto the
challenge...
a) 我使用准备好的语句从使用 ArrayList
作为输入的表中选择数据为了这.
a) I am using a prepared statement to select the data from a table using the ArrayList
as input
for this.
selectPS = dbConnection.prepareStatement("select columnA from tableA where id in ?");
上面的问题 - 应该如何定义参数?以上似乎不正确ArrayList
类型参数.
Question on the above - how should the parameter be defined? The above does not seem correct for
an ArrayList
type parameter.
b) 在为准备好的参数设置参数值时,我也遇到了问题陈述.没有设置 ArrayList
类型值的方法,我看不到其他可行的方法选项.
b) I am also running into problems when setting the value of the parameter for the prepared
statement. There is no method for setting an ArrayList
type value and I see no other viable
options.
---> selectPS.set?????(1, arraylistParameter);
ResultSet rs = selectPS.executeQuery();
非常感谢您能给我提供的任何帮助或指导.
Any help or direction you can set me in is greatly appreciated.
谢谢.
推荐答案
您可能想要使用 setArray
方法,如下面的 javadoc 中所述:
You may want to use setArray
method as mentioned in the javadoc below:
示例代码:
PreparedStatement pstmt =
conn.prepareStatement("select * from employee where id in (?)");
Array array = conn.createArrayOf("VARCHAR", new Object[]{"1", "2","3"});
pstmt.setArray(1, array);
ResultSet rs = pstmt.executeQuery();
这篇关于如何使用数组列表作为准备好的语句参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!