• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Connecting JDBC

How do you create a Oracle supported table from java using JDBC ?!
0
dhanan77
Asked:
dhanan77
  • 3
1 Solution
 
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
 
OviCommented:
If you don't want to listen...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now