//********************************************************************* // // Copyright (c) 2006-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20002JD.java // Header: none // Purpose: Demonstrate basic Teradata DDL. // The program will: // - Connect as user guest/please // - Drop table identityColumn if it exists // - Create table books with the following columns: // ISBN (VARCHAR(13)), // title (VARCHAR(100)), // author (VARCHAR(50)), // pulisher (VARCHAR(100)), // idCol (INTEGER GENERATED ALWAYS AS IDENTITY) // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.Statement, // java.sql.Statement.executeUpdate // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T20002JD { // Name of the user able to create, drop, and manipulate tables public static String sUser = "guest"; public static String sPassword = "please"; public static void main(String args[]) throws ClassNotFoundException { // Creation of URL to be passed to the JDBC driver String url = "jdbc:teradata://whomooz/TMODE=ANSI,CHARSET=UTF8"; // Statements used in table creation String sDropTbl = "DROP TABLE books"; String sCreateTbl = "CREATE TABLE books ( "+ "ISBN VARCHAR(13), "+ "bookTitle VARCHAR(100), "+ "bookAuthor VARCHAR(50) Default 'Unknown', "+ "bookPublisher VARCHAR(100) Default 'Advanced Books', "+ "identityCol INTEGER GENERATED ALWAYS AS IDENTITY"+ " (START WITH 1 INCREMENT BY 1)) "+ "PRIMARY INDEX ( ISBN )"; try { System.out.println("\n Sample T20002JD: \n"); System.out.println(" Looking for the Teradata JDBC driver... "); // Loading the Teradata JDBC driver Class.forName("com.teradata.jdbc.TeraDriver"); System.out.println(" JDBC driver loaded. \n"); // Attempting to connect to Teradata System.out.println(" Attempting to connect to Teradata via" + " the JDBC driver..."); // Creating a connection object Connection con = DriverManager.getConnection(url, sUser, sPassword); System.out.println(" User " + sUser + " connected."); System.out.println(" Connection to Teradata established. \n"); try { // Creating a statement object from an active connection. Statement stmt = con.createStatement(); System.out.println(" Statement object created. \n"); try { // Cleanup procedure: // If the sample table already exists, drop it. try { System.out.println(" Dropping table if present: " + sDropTbl); stmt.executeUpdate(sDropTbl); System.out.println(" Table dropped.\n"); } catch (SQLException ex) { // If the table did not exist, no drop is required. // Ignore the raised "no table present" exception by // printing out the error message and swallowing the // exception. System.out.println(" Drop table exception ignored: " + ex); System.out.println(" Table could not be dropped." + " Execution will continue...\n"); } // Create the sample table System.out.println(" Creating table: " + sCreateTbl); stmt.executeUpdate(sCreateTbl); System.out.println(" Sample table created. \n"); } finally { // Close the statement stmt.close(); System.out.println("\n Statement object closed. \n"); } } finally { // Close the connection System.out.println(" Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. \n"); } System.out.println(" Sample T20002JD finished. \n"); } catch (SQLException ex) { // A SQLException was generated. Catch it and display // the error information. // Note that there could be multiple error objects chained // together. System.out.println(); System.out.println("*** SQLException caught ***"); while (ex != null) { System.out.println(" Error code: " + ex.getErrorCode()); System.out.println(" SQL State: " + ex.getSQLState()); System.out.println(" Message: " + ex.getMessage()); ex.printStackTrace(); System.out.println(); ex = ex.getNextException(); } throw new IllegalStateException ("Sample failed.") ; } } // End main } // End class T20002JD