Putting It All Together
Example 15-1 puts all the concepts and short examples
we’ve seen in this chapter into one cohesive example.
Here’s the big picture: the TestStruct
program starts by cleaning up any rows left over from a prior
execution. Then it adds a person and a
location and ties them together with a
person_location entry so that there are foreign
key constraints on the person and
location objects. Next, the program modifies the
person object using both the
PreparedStatement object’s
setObject( ) method and the
oracle.sql.REF object’s setValue( ) method. Finally, the program selects the
person object and displays its contents. Since the
person object has a collection for identifiers,
the example exercises not only oracle.sql.STRUCT,
but also oracle.sql.ARRAY. Let’s continue by
examining the TestStruct program in detail.
Example 15-1. Testing weakly typed JDBC object types
import java.io.*; import java.math.*; import java.sql.*; import java.text.*; public class TestStruct { Connection conn; public TestStruct( ) { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver( )); conn = DriverManager.getConnection( "jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger"); } catch (SQLException e) { System.err.println(e.getMessage( )); e.printStackTrace( ); } } public static void main(String[] args) throws Exception { new TestStruct().process( ); } public void process( ) throws SQLException { // PERSON_TYP attributes final int PT_PERSON_ID = 0; final int PT_LAST_NAME ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access