How To Create Oracle Table for storing Java Types?

Below Java code shows, how to create oracle type for storing Java Types –

/****************************************************************************************
 * Created on 05-2011 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javasql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by http://kodehelp.com
 * Date: 5/2/11
 */
public class OracleTableForJavaTypes {
    public static void main(String args[]) throws ClassNotFoundException, SQLException {
         final String driver = "oracle.jdbc.driver.OracleDriver";
        final String url = "jdbc:oracle:thin:@HOST:PORT:SID";
        final String USERNAME = "USERNAME";
        final String PASSWORD = "PASSWORD";
        Class.forName(driver); // load Oracle driver
        Connection conn = DriverManager.getConnection(url, USERNAME, PASSWORD);

        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TYPE number_varray AS VARRAY(10) OF NUMBER(12, 2)");
        stmt.execute ("CREATE TYPE my_object AS OBJECT(col_string2 VARCHAR(30), col_int2 INTEGER)");

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

        if(stmt!=null){
            stmt.close();
        }

        if(conn!=null){
            conn.close();
        }
    }
}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.