The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Tables  [6 examples]

e249. Creating an Oracle Table to Store Java Types

This example creates an Oracle table called oracle_all_table to store Java types.

Oracle does not support the boolean Java type directly. A typical workaround is to use the Oracle CHAR(1) type and convert the boolean value to and from a string. Typically, false is converted to F and true is converted to T.

    try {
        Statement stmt = connection.createStatement();
    
        // Create a VARRAY type; see e301 Creating a VARRAY Type in an Oracle Database
        stmt.execute("CREATE TYPE number_varray AS VARRAY(10) OF NUMBER(12, 2)");
    
        // Create an OBJECT type; e296 Creating an OBJECT Type in an Oracle Database
        stmt.execute ("CREATE TYPE my_object AS OBJECT(col_string2 VARCHAR(30), col_int2 INTEGER)");
    
        // Note that Oracle database only allows at most one column of LONG type in a table.
        //    Column Name          Oracle Type             Java Type
        String sql = "CREATE TABLE oracle_all_table("
            + "col_short           SMALLINT, "          // short
            + "col_int             INTEGER, "           // int
            + "col_float           REAL, "              // float; can also be NUMBER
            + "col_double          DOUBLE PRECISION, "  // double; can also be FLOAT or NUMBER
            + "col_bigdecimal      DECIMAL(13,0), "     // BigDecimal
            + "col_string          VARCHAR2(254), "     // String; can also be CHAR(n)
            + "col_characterstream LONG, "              // CharacterStream or AsciiStream
            + "col_bytes           RAW(2000), "         // byte[]; can also be LONG RAW(n)
            + "col_binarystream    RAW(2000), "         // BinaryStream; can also be LONG RAW(n)
            + "col_timestamp       DATE, "              // Timestamp
            + "col_clob            CLOB, "              // Clob
            + "col_blob            BLOB, "              // Blob; can also be BFILE
            + "col_array           number_varray, "     // oracle.sql.ARRAY
            + "col_object          my_object)";         // oracle.sql.OBJECT
    
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
    }

 Related Examples
e245. Creating a Database Table
e246. Deleting a Database Table
e247. Listing All Table Names in a Database
e248. Creating a MySQL Table to Store Java Types
e250. Creating a SQLServer Table to Store Java Types

See also: Batching    Connections    Database Meta Data    Deleting Data    Drivers    Importing and Exporting    Inserting and Updating Data    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Retrieving Data    Scrollable Result Sets    Updatable Result Sets   


© 2002 Addison-Wesley.