T20201JD.java





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

import java.sql.*;

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

        // Strings representing a prepared statement and its parameter values,
        // respectively
        String sInsert = "INSERT INTO employee VALUES(?,?,?,?)";
        int Id = 100008;
        String Name = "Robert Martinez";
        String Department = "Human Resources";
        String Description = "Recruiter in charge of university relations";

        try
        {
            System.out.println(" Sample T20201JD: ");
            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 PreparedStatement object from an active
                // connection. A PreparedStatement is an object that represents
                // a precompiled SQL statement and allows for efficient
                // execution of the precompiled SQL statement multiple times.
                System.out.println(" Preparing this SQL statement for execution: "
                                   + sInsert);
                PreparedStatement pstmt = con.prepareStatement(sInsert);
                System.out.println(" Prepared statement object created. ");

                try
                {
                    int RowCount;     // Return value for row count

                    // The following code will perform an INSERT into the table
                    // using a prepared statement using the
                    // PreparedStatement.executeUpdate method to execute SQL.
                    System.out.println(" Attempting an insertion...");

                    // Set parameter values indicated by ? (dynamic update)
                    // PreparedStatement.setInt and PreparedStatement.setString
                    // methods will be demonstrated. Please refer to the
                    // supporting driver documentation for the full list of
                    // methods, their implementations, and their return values.
                    System.out.println(" Using setInt() and setString() to " +
                                       "assign values to parameter markers:");
                    pstmt.setInt(1, Id);
                    System.out.println(" FIRST ? set to: " + Id);
                    pstmt.setString(2, Name);
                    System.out.println(" SECOND ? set to: " + Name);
                    pstmt.setString(3, Department);
                    System.out.println(" THIRD ? set to: " + Department);
                    pstmt.setString(4, Description);
                    System.out.println(" FOURTH ? set to: " + Description);

                    // Once all of the parameter values have been set, call
                    // executeUpdate to execute the now complete SQL command.
                    // Display row count returned by the method.
                    RowCount = pstmt.executeUpdate();
                    System.out.println(" Insertion completed successfully: "
                                       + RowCount + " row(s) inserted.");
                }
                finally
                {
                    // Close the statement
                    pstmt.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 T20201JD 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 T20201JD



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