Connecting JDBC

How do you create a Oracle supported table from java using JDBC ?!
dhanan77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OviCommented:
With java.Sql ? I think so, dont'you ?
0
g_senthilkumarCommented:
I dont think you can create a table from java pgm.
0
OviCommented:
what about this SQL statement :
CREATE TABLE TEST (ID fixed(4) KEY, Name char(30), UNIQUE (Name))
maybe your problem is the driver.
0
AMD_MANCommented:

dhanan77,

You can create a table from jdbc using executeUpdate.  I have provided a working example that runs on a Solaris box.  Oracle 8.0.5 is the DB.  It is from _Java Enterprise in a Nutshell_ as it states in the header comments.  I modified it to match your conditions.

You can use something like getSQLKeywords to find if CREATE is supported on your system I assume...

Later,
AMD_MAN

The code is below:


/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import java.sql.*;

public class JDBCSample {

  public static void main(java.lang.String[] args) {
    try {
      // This is where we load the driver
                        Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    catch (ClassNotFoundException e) {
      System.out.println("Unable to load Driver Class");
      return;
    }
 
    try {
      // All database access is within a try/catch block. Connect to database,
      // specifying particular database, username, and password
                        Connection con = DriverManager.getConnection("jdbc:oracle:oci8:@pc1", "pc", "pc");
   
      // Create and execute an SQL Statement
        Statement stmt = con.createStatement();

        stmt.executeUpdate("create table TEST_TABLE ( ID_NUMBER NUMBER(38) NOT NULL, NAME_FIELD VARCHAR2(25), UNIQUE(ID_NUMBER), PRIMARY KEY (ID_NUMBER))");

      // Make sure our database resources are released
      stmt.close();
      con.close();

    }
    catch (SQLException se) {
      // Inform user of any SQL errors
      System.out.println("SQL Exception: " + se.getMessage());
      se.printStackTrace(System.out);
    }
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OviCommented:
If you don't want to listen...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.