這是我的查詢。如何填寫PreparedStatement中的WHERE IN參數
String SELECT_USERS_FROM_GROUPS = "select * from user where group_id in ?";
,我需要選擇從那來的一個列表組用戶:
例如,該列表可以。
long[] groupIdList = { 1, 2 };
這裏是我的代碼:
public List<User> getUsersFromGroups(long[] groupIdList) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
List<User> userList = null;
User user;
try {
connection = Connector.getConnection();
statement = connection.prepareStatement(SELECT_USERS_FROM_GROUPS);
Array groupIdArray = connection.createArrayOf("LONG", groupIdList);
statement.setArray(1, groupIdArray);
rs = statement.executeQuery();
userList = new ArrayList<User>();
while (rs.next()) {
user = new User();
user = fillUser(rs);
userList.add(user);
}
} catch (SQLException e) {
logger.error(e.getMessage(), e);
} finally {
ResourcesUtil.release(rs, statement, connection);
}
return userList;
}
但我得到一個異常試圖線:Array groupIdArray = connection.createArrayOf("LONG", groupIdList);
有人可以幫我改正錯的,或引導到另一個可能的解決方案。 謝謝;
- 編輯
例外:
ERROR UserDao:138 -
java.sql.SQLFeatureNotSupportedException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.mysql.jdbc.SQLError.notImplemented(SQLError.java:1350)
at com.mysql.jdbc.JDBC4Connection.createArrayOf(JDBC4Connection.java:55)
at com.mchange.v2.c3p0.impl.NewProxyConnection.createArrayOf(NewProxyConnection.java:589)
at com.thehuxley.data.dao.UserDao.getUsersFromGroups(UserDao.java:120)
發佈例外。 – proulxs 2014-09-29 17:37:17
@proulxs編輯 – MariaH 2014-09-29 17:42:25
沒有數組超出界限異常嗎?!! – 2014-09-29 17:43:54