1

I'm using JDBC, I have to set array of values to a single column, I know it works in Hibernate and Ibatis but it seems to be hard to get it working Pure JDBC sql.

I have an array of String values

names[] = new String[]{"A","B","C"};

and a Query like

select * from emp where name in(?)

I tried pstmt.setObject(1,names), it is not working..

1
  • You might get there if you create your query string dynamically and add ?s for each item in your array, and then bind each one of them to your query. Commented Jan 9, 2014 at 12:30

2 Answers 2

1

This is not supported in pure JDBC. You have to generate a query so that the in clause contains one placeholder for each element of the array.

Spring's JDBC helper classes support named parameters and what you want to do.

Sign up to request clarification or add additional context in comments.

1 Comment

I think it is supported in JDBC now - see my answer!
0

This will work with the following Syntax:

"SELECT * FROM emp WHERE name IN ( SELECT column_value FROM TABLE( ? ) )"

pmst.setArray( 1, conn.createArrayOf( "VARCHAR", names ) );

2 Comments

Note that createArrayOf might not work across all major relational databases. See: stackoverflow.com/q/28400518/59087
There are several tricks to get it to work with other Vendors, But it is a hassle you need a separate case for each DBMS...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.