MariaH
MariaH

Reputation: 341

How to fill WHERE IN parameter in PreparedStatement

That is my query.

String SELECT_USERS_FROM_GROUPS = "select * from user where group_id in ?";

I need to select users from groups that come in a list:

For example, the list could be.

long[] groupIdList = { 1, 2 };

Here is the my code:

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;

    }

But I get an exception trying line: Array groupIdArray = connection.createArrayOf("LONG", groupIdList);

Can somebody help me to correct what is wrong, or guide to another possible solution. Thank you;

-- EDIT

The exception:

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)

Upvotes: 1

Views: 3585

Answers (2)

Durandal
Durandal

Reputation: 20059

JDBC prepared statements only support IN clauses with a known number of arguments, each value must be represented in the original query:

select * from user where group_id in (?, ?)

Parameters are set just like any other parameters using statement.setXXX methods. If you need a variable number of parameters, you must generate the query string dynamically (providing the correct number of ? in the IN clause).

See also: PreparedStatement IN clause alternatives?

Upvotes: 1

proulxs
proulxs

Reputation: 508

The SQLFeatureNotSupportedException is thrown when:

The JDBC driver does not support this data type

So looks like your database does not support the "LONG" datatype.

Upvotes: 0

Related Questions