T20001JD.java





T20001JD.java Expand / Collapse
//************************************************************************************
//
//                 Copyright (c) 2004-2008 by Teradata Corporation
//                         All Rights Reserved
//
//                   TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
//
//************************************************************************************
//
//  File:       T20001JD.java
//  Header:     none
//  Purpose:    Demonstrate basic Teradata DDL.
//              The program will:
//                -  Connect as user guest/please
//                -  Drop table employee2 if it exists
//                -  Create table employee2 with the following columns:
//                       empID (INTEGER),
//                       empName (VARCHAR(30)),
//                       empDept (VARCHAR(50)),
//                       empJob (VARCHAR(300)),
//                       empResume (CLOB),
//                       empPhoto (BLOB),
//                       empRawPhotoData (BYTEINT)
//                -  Create two table indexes: unique and non-unique
//                -  Disconnect.
//
//  JDBC API: java.sql.Connection, java.sql.Statement,
//            java.sql.Statement.executeUpdate
//
//  Version: Updated for Teradata V2R6
//
//************************************************************************************

import java.sql.*;

public class T20001JD
{
    // 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 employee2";
        String sCreateTbl = "CREATE TABLE employee2 (empID INTEGER NOT NULL, " +
                "empName VARCHAR(30) NOT NULL, empDept VARCHAR(50) NOT NULL, " +
                "empJob VARCHAR(300), empResume CLOB, empPhoto BLOB, " +
                "empRawPhotoData BYTEINT DEFAULT 0, " +
                "CHECK (empRawPhotoData = 0 OR empRawPhotoData = 1), " +
                "PRIMARY KEY(empID))";

        // Statements used in index creation. Both unique and non-unique indexes
        // will be created. Please note that these may not result in optimal
        // performance.
        String sCreateIdx = "CREATE INDEX (empName) ON employee2";
        String sCreateIdx2 = "CREATE UNIQUE INDEX (empName, empDept) ON employee2";

        try
        {
            System.out.println(" Sample T20001JD: ");
            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. ");

            // 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. ");

            try
            {
                // Creating a statement object from an active connection.
                Statement stmt = con.createStatement();
                System.out.println(" Statement object created. ");

                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.");
                    }
                    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...");
                    }

                    // Create the sample table
                    System.out.println(" Creating table: " + sCreateTbl);
                    stmt.executeUpdate(sCreateTbl);
                    System.out.println(" Sample table created. ");
                    // Create table indexes
                    System.out.println(" Creating table indexes: "
                                       + sCreateIdx + " " + sCreateIdx2);
                    stmt.executeUpdate(sCreateIdx);
                    stmt.executeUpdate(sCreateIdx2);
                    System.out.println(" Table indexes created.");
                }
                finally
                {
                    // Close the statement
                    stmt.close();
                    System.out.println(" Statement object closed. ");
                }
            }
            finally
            {
                // Close the connection
                System.out.println(" Closing connection to Teradata...");
                con.close();
                System.out.println(" Connection to Teradata closed. ");
            }

            System.out.println(" Sample T20001JD finished. ");
        }
        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 T20001JD



Questions or Feedback
Contact Us
 
Related Resources
Teradata Developer Exchange
Teradata Discussion Forums
White Papers
Teradata Support Services
Teradata User Groups


Find more downloads (including early access and unsupported releases) on Teradata Developer Exchange.
Company Newsroom Site Help Site Map Privacy/Legal Contact Us