Create Table JDBC Example

Create Table JDBC

In the following Create Table JDBC code, a new Employee table in Oracle database is created. The Employee table with fields empid, empname and empsal is created using DSN snrao.

Note: Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

import java.sql.*;
public class CreateTable
{
  public static void main(String args[])
  { 
    Connection con = null;
    Statement stmt = null;

    try
    {			                             // load the driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                                     // establish the connection
      con = DriverManager.getConnection("jdbc:odbc:snrao",  "scott", "tiger");
                                                     // create Statement object
      stmt = con.createStatement();
                                                     // send sql command
      stmt.executeUpdate("create table Employeee(empid number, empname varchar2(15), empsal number(6, 2))");  // Create Table JDBC

      System.out.println("Table created");
    }
    catch(ClassNotFoundException e)
    {
      System.err.println("Driver is not found. " + e);
    }
    catch(SQLException e)
    {
      System.err.println("Some problem in creating connection. " + e);
    }
    finally
    {                                                // close the database connection
      try
      {
        stmt.close();
        con.close();
      }
      catch(SQLException e)
      {
        System.err.println("Unable to close. " + e);
      }
    }
  }
} 

The above program can be simplified by throwing the exception. This approach is not used in realtime. Of course, realtime varies a lot where connection pooling and Hibernate are used; but knowledge of this JDBC code is required to understand Hibernate framework.

import java.sql.*;
public class CreateTable
{
  public static void main(String args[]) throws Exception
  {                                                  // load the driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                                     // establish the connection
    Connection con = DriverManager.getConnection("jdbc:odbc:snrao",  "scott", "tiger");
                                                     // create Statement object
    Statement stmt = con.createStatement();
                                                     // send sql command
    stmt.executeUpdate("create table Employee(empid number, empname varchar2(15), empsal number(6, 2))");  // Create Table JDBC

    System.out.println("Table created");
                                                     // close the database connection
    stmt.close();
    con.close();
  }
}

In all our subsequent programs, this approach is used.

4 thoughts on “Create Table JDBC Example”

  1. sir, i am getting the following error when i executed JDBC program in eclipse

    “name is already used by an existing object…..”

Leave a Comment

Your email address will not be published.