【发布时间】:2014-11-24 04:24:20
【问题描述】:
这是我的问题。
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)
【问题讨论】:
-
没有数组超出边界异常?!!
-
此异常表示数据库或驱动程序不支持此功能。
-
您使用的是哪个数据库/驱动程序?我熟悉的大多数数据库都以某种形式支持“Long”类型。 createArrayOf 的第一个参数是数据库识别的 SQL 类型名称,因此您可能只需要更改您传递的第一个参数。
-
您使用的是哪个 dbm? mysql 不支持数组,按照stackoverflow.com/questions/17842211/… 见第二个答案的解决方案。
-
我尝试将 LONG 更改为 BIGINT。它也没有工作。 @strwils
标签: java prepared-statement where-in