我有一个arraylist。 arraylist包含特定表中的列值。例如,'name'是表格的一列。它具有arraylist中的xxx,yyy,zzz值。我的问题是,如何在arraylist的'in子句'中添加名称。
Arraylist<String> list = new ArrayList<String>();
list.add('xxx');
list.add('yyy');
list.add('zzz');
Select * from table where name in('xxx','yyy','zzz');
答案 0 :(得分:0)
I'm not sure if this is possible with the PreparedStatement, but you can use Statement here:
Statement stmt = conn.createStatement();
String req = "Select * from table where name in(";
String params = "";
for (int i = 0; i < list.size(); i++) {
if (!params.isEmpty())
params += ",";
params += "'"+list.get(i)+"'";
}
req += params + ")";
ResultSet rs = stmt.executeQuery(req);