Software Development

Developing with OpenSource and Microsoft Technologies

How to create an oracle table with Java

with one comment

I ‘ve created  a Connection Class which has methods  like getConnection, createStatement,  executeQuery, executeNonQuery, etc.

import java.sql.*;

public class OracleConnection
{

      private static Connection _conn = null;

    public static  Connection GetConnection ()
    {
        try{
      oracle.jdbc.pool.OracleDataSource _ds  = new oracle.jdbc.pool.OracleDataSource();
              _ds.setDriverType("thin");
	      _ds.setServerName("localhost");
	      _ds.setPortNumber(1521);
	      _ds.setDatabaseName("BDI");
	      _ds.setUser("SYSTEM");
	      _ds.setPassword("admin");

              Class.forName("oracle.jdbc.OracleDriver") ;

        _conn = _ds.getConnection();

    }catch(SQLException e)
    {
        System.err.println(e.getMessage());

    }
        catch(Exception e)
    {
        System.err.println(e.getMessage());

    }
        finally
        {
             return _conn;
        }

    }

    public static  Statement CreateStatement(Connection conn)
    {
         Statement _command= null;
        try
        {
        _command = conn.createStatement();
        }catch(SQLException e)
        {
        System.err.println(e.getMessage());
        }

        return _command;
    }

    public static  ResultSet ExecuteQuery(Statement command,String query)
    {

        ResultSet _table= null;
        try
        {
        _table= command.executeQuery(query);
        command.close();
        }catch(SQLException e)
        {
        System.err.println(e.getMessage());
        }

        return  _table;
    }

    public static  void ExecuteNonQuery(Statement command,String query)
    {
        try
        {
            command.executeUpdate(query);
            command.close();
        }
        catch(SQLException e)
        {
            System.err.println(e.getMessage());
        }
    }

}

I will use this class to create a table in the main class passing the query through a parameter.

import java.sql.*;

public class CreateDatabase {

   private static  Connection _conn= null;
   private static  Statement _command = null;

   public static void main(String[] args) throws SQLException {

       try{
     _conn= OracleConnection.GetConnection();
     _command= OracleConnection.CreateStatement(_conn);
     String query ="CREATE TABLE DEPARTMENT (" +
             " ID INTEGER PRIMARY KEY," +
             " NAME VARCHAR(20) NOT NULL,"+
             " DESCRIPTION VARCHAR(100))";

    OracleConnection.ExecuteNonQuery(_command, query);
     _conn.close();
       }
       catch(SQLException e)
       {
       System.err.println(e.getMessage());
       }

   }
}

Result:

run:

BUILD SUCCESSFUL (total time: 1 second)

JavaOracle08

Advertisements

Written by mredison

July 19, 2009 at 10:46 pm

Posted in Java, Oracle

One Response

Subscribe to comments with RSS.

  1. Congratulations for this new blog, I’m also following at mredison.wordpress.com,and this blog, will go to my bloglines too:D

    Good Luck!!

    Pp

    July 22, 2009 at 3:24 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: