T20200JD.java





T20200JD.java Expand / Collapse
//************************************************************************************
//
//                 Copyright (c) 2004-2008 by Teradata Corporation
//                         All Rights Reserved
//
//                   TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
//
//************************************************************************************
//
//  File:       T20200JD.java
//  Header:     none
//  Purpose:    Demonstrate processing of basic INSERT statements
//              without using parameter markers.
//              The program will:
//                -  Connect as user guest/please
//                -  Perform a series of insertions into the table
//                -  Disconnect.
//
//  JDBC API: java.sql.Connection,
//            java.sql.Statement, java.sql.Statement.executeUpdate
//
//  Version: Updated for Teradata V2R6
//
//************************************************************************************

import java.sql.*;

public class T20200JD
{
    // 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";

        // Preparing various insertion commands
        String[] sInsert =
            {"INSERT INTO employee VALUES(100001, 'Mike Smith',"
            + " 'Product Development', 'QA engineer responsible for planning,"
            + " developing and maintaining frameworks for test automation')",
            "INSERT INTO employee VALUES(100002, 'James Parker',"
            + " 'Marketing', 'Manager coordinating international sales')",
            "INSERT INTO employee VALUES(100003, 'Mary Jones',"
            + " 'Product Development', 'Software engineer responsible for"
            + " regression test plans')",
            "INSERT INTO employee VALUES(100004, 'John Walker',"
            + " 'Human Resources', 'Manager responsible for employee"
            + " benefits programs')",
            "INSERT INTO employee VALUES(100005, 'Steven Brown',"
            + " 'Customer Service', 'Software engineer providing technical"
            + " applications support')",
            "INSERT INTO employee VALUES(100006, 'Susan Young',"
            + " 'Product Development', 'QA engineer overlooking overseas testing')",
            "INSERT INTO employee VALUES(100007, 'Brian Lee', 'Marketing',"
            + " 'Team leader managing market research')"};

        try
        {
            System.out.println(" Sample T20200JD: ");
            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
                {
                    int RowCount;     // Return value for row count

                    // The following code will demonstrate performing a series
                    // of insertions into the table using the SQL INSERT
                    // statements. Numerous inserts are performed here to aid in
                    // subsequent examples.

                    // Retrieve the number of insertions to be performed
                    int len = sInsert.length;
                    // Perform insertions and display row counts as returned
                    // by the Statement.executeUpdate method.
                    for (int i = 0; i < len; i++)
                    {
                        System.out.println(" Attempting an insertion: "
                                           + sInsert[i]);
                        RowCount = stmt.executeUpdate(sInsert[i]);
                        System.out.println(" Insertion completed successfully: "
                                           + RowCount + " row(s) inserted.");
                    }
                }
                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 T20200JD 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 T20200JD



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