DEVFYI - Developer Resource - FYI

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

JDBC Interview Questions and Answers


(Continued from previous question...)

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

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 );

(Continued on next question...)

Other Interview Questions