How can I convert a java array to a java.sql.Array?

Joe Sam Shirah

You may want to first review JDBC(tm) Technology Guide: Getting Started, Section 8: Mapping SQL and Java Types. You'll see that the JDBC ( java.sql ) Array is consistently mapped to an array. Unfortunately, capitalization not particularly enforced there.

However, in some sense, one never converts from an array to a java.sql.Array, which is an interface for retrieving and materializing an SQL3 ARRAY data type. A Java array is a first class object and all of the references basically use PreparedStatement.setObject() or ResultSet.updateObject() methods for putting the array to an ARRAY in the database. Here's a basic example:

String[] as = { "One", "Two", "Three" };
PreparedStatement ps = con.prepareStatement( 
  "UPDATE MYTABLE SET ArrayNums = ? WHERE MyKey = ?" );

ps.setObject( 1, as );

For more information see: Array and Using SQL3 Datatypes.