Skip to content

Cannot use setArray JDBC (integer) with H2 Database

I want to put an array of integers in my jdbc query with H2 database.

Integer[] list = new Integer[]{1,2,3};
            String query = "SELECT EXAMPLE FROM DATA WHERE EXAMPLE IN (?)";
            PreparedStatement ps = GestionBDD.getConexionBD().prepareStatement(query);
            Array array = GestionBDD.getConexionBD().createArrayOf("int", list);

            ps.setArray(1, array);

            ResultSet rs = ps.executeQuery();

            while ( {
                // DO THINGS 

But doest not work, I am getting this exception : org.h2.jdbc.JdbcSQLException: Data conversion error converting “(1,2,3)”; SQL statement:

I am using H2 database. Can you help me please?



Using my JDBC utilities to work with JDBC IN array parameters easily:

1. If Maven, add the below dependency into your pom.xml


2. If no maven, download this jar file

3. Fix your code ( Works all DBMS guaranteed )

static final Sql EXAMPLE_SQL 
          = new Sql("SELECT EXAMPLE FROM DATA WHERE EXAMPLE IN :example_array");

StatementImpl stat = new StatementImpl(GestionBDD.getConexionBD(), EXAMPLE_SQL);

stat.setIntArray("example_array", new int[] {1, 2, 3} );

ResultSet rs = stat.executeQuery();

while ( {
    // DO THINGS 

rs.close(); stat.close();

4. Named parameters / Null parameters

final Sql sampleSql 
       = new Sql("SELECT * FROM Table WHERE a=:int_a AND b=:str_b)

StatementImpl stat = new StatementImpl(conn, sampleSql);

stat.setInt("int_a", int_value);
stat.setInt2("int_a", null_value);
stat.setString("str_b", str_value);
// ...

5. Some Notes

  • Parameter name supported. No more ? in your query
  • Parameter in Sql need to start with : (Like JPA parameters)
  • IN Array parameter in Sql must be in this syntax IN :param_name
User contributions licensed under: CC BY-SA
4 People found this is helpful