如何使用一个ArrayList为prepared语句参数 [英] How to use an arraylist as a prepared statement parameter

查看:220
本文介绍了如何使用一个ArrayList为prepared语句参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过,并一直未能找到一个答案,我有以下挑战。它
看来pretty简单,但我一直无法予以解决。

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.

我有记录ID是键入的ArrayList - > 的ArrayList<朗GT&; 。我想用
记录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...

一)我使用的是prepared语句中使用的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 类型参数。

二)我也遇到了问题,为prepared设置参数的值时,
   声明。有一个设置一个的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(); 

任何帮助或方向可以设置我是极大的AP preciated。

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:

<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/sql/$p$pparedStatement.html#setArray%28int,%20java.sql.Array%29\">http://docs.oracle.com/javase/6/docs/api/java/sql/$p$pparedStatement.html#setArray(int,了java.sql.Array)

样code:

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();

这篇关于如何使用一个ArrayList为prepared语句参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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