How to Create Oracle Table in Java?

DeeKay May 2, 2011 at 6:34 pm · java.sql

Below Java code shows, how to create Oracle Table using JDBC Driver –

/****************************************************************************************
 * 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 CreateOracleTable {
    public static void main(String args[]) throws SQLException, ClassNotFoundException {
        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);
        String sql = "CREATE TABLE PRODUCTS (id NUMBER(11), NAME VARCHAR2(64))";
        Statement statement = conn.createStatement();
        statement.execute(sql);
        conn.close();
    }
}