A Sample JDBC Program


The following sample program is modified from a program in the page Sample Applications. Many JDBC program examples can be found from there. This sample demonstrates the functionality of the Oracle class oracle.sql.REF for weakly typed support of SQL object references. It defines the SQL object type student and uses references to that object type. This program includes the following SQL commands:

 SQL> CREATE TYPE student AS OBJECT (
   2    ID INTEGER, name VARCHAR(32), age INTEGER );
   3  /
 Type created.

 SQL> CREATE TABLE student_table OF student;
 Table created.

 SQL> CREATE SEQUENCE userid_seq MINVALUE 1
   2    START WITH 1 INCREMENT BY 1 NOCACHE;
 Sequence created.

 SQL> INSERT INTO student_table
   2    SELECT userid_seq.NEXTVAL, 'Digi Mon', 10 FROM DUAL;
 1 row created.


    Drop   Create   Insert   Select   (args[0])

     Name (args[1]) = [for insert and select (listing all if empty)]

     Age (args[2]) = (for insert)

                


// Import the following packages to use JDBC.
import  java.sql.*;
import  java.sql.DatabaseMetaData;
import  java.io.*;
import  java.math.BigDecimal;
import  oracle.sql.*;
import  oracle.jdbc.*;
import  oracle.jdbc.pool.OracleDataSource;

class  StudentRef {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##user.id";
    String password = "password";
    String database = "65.52.222.73:1521/cdb1";

    // Open an OracleDataSource and get a connection.
    OracleDataSource ods = new OracleDataSource( );
    ods.setURL     ( "jdbc:oracle:thin:@" + database );
    ods.setUser    ( user );
    ods.setPassword( password );
    Connection conn = ods.getConnection( );
	
    String     cmd;
    Statement  stmt = conn.createStatement( );
    ResultSet  rset;
    DatabaseMetaData  dbm = conn.getMetaData( );

    if ( args[0].equals( "drop" ) ) {
      cmd  = "drop sequence userid_seq";
      System.out.println( cmd );
      stmt.execute( cmd );
      cmd  = "drop table student_table";
      System.out.println( cmd );
      stmt.execute( cmd );
      cmd  = "drop type STUDENT";
      System.out.println( cmd );
      stmt.execute( cmd );
    }

    else if ( args[0].equals( "create" ) ) {
      // Check if "student_table" table is there.
      rset = dbm.getTables(
               null, "C##user.id", "STUDENT_TABLE", new String[] {"TABLE"} );
      if ( rset.next( ) ) {
        // Table does exist.
        cmd  = "drop sequence userid_seq";
        System.out.println( cmd );
        stmt.execute( cmd );
        cmd  = "drop table student_table";
        System.out.println( cmd );
        stmt.execute( cmd );
        cmd  = "drop type STUDENT";
        System.out.println( cmd );
        stmt.execute( cmd );
      }
      cmd  = "create sequence userid_seq minvalue 1 ";
      cmd += "start with 1 increment by 1 nocache";
      System.out.println( cmd );
      stmt.execute( cmd );

      cmd  = "create type STUDENT as object ";
      cmd += "( ID integer, name varchar(32), age integer )";
      System.out.println( cmd );
      stmt.execute( cmd );

      cmd  = "create table student_table of STUDENT";
      System.out.println( cmd );
      stmt.execute( cmd );

      cmd  = "insert into student_table ";
      cmd += "select userid_seq.NEXTVAL, 'Digi Mon', 10  from dual";
      System.out.println( cmd );
      stmt.execute( cmd );
      rset.close( );
    }

    else if ( args[0].equals( "insert" ) ) {
      cmd  = "insert into student_table ";
      cmd += "select userid_seq.NEXTVAL, '" + args[1].trim( );
      cmd += "', " + args[2].trim( ) + " from dual";
      System.out.println( cmd );
      stmt.execute( cmd );
    }

    else if ( args[0].equals( "select" ) ) {
      cmd  = "select ref(s) from student_table s where ";
      cmd += "name like '%" + args[1].trim( ) + "%'";
      System.out.println( cmd );
      rset = stmt.executeQuery( cmd );

      while ( rset.next( ) ) {
        // Retrieve the ref object.
        REF ref = (REF) rset.getObject( 1 );
        // Retrieve the object value that the ref points to in the
        //   object table.
        STRUCT student = (STRUCT) ref.getValue( );
        Object attributes[] = student.getAttributes( );
        System.out.println( "\nStudent ID: "  +
          ((BigDecimal) attributes[0]).intValue( ) );
        System.out.println( "Student name: " + 
          (String) attributes[1] );
        System.out.println("Student age:  "  +
          ((BigDecimal)	attributes[2]).intValue( ) );
      }  
      rset.close( );
    }
    stmt.close( );
    conn.close( );
  }
}




      “Never let your sense of morals prevent you from doing what is right.”    
      ― Isaac Asimov, Foundation