T20600JD.java





T20600JD.java Expand / Collapse
//************************************************************************************
//
//                 Copyright (c) 2004-2008 by Teradata Corporation
//                         All Rights Reserved
//
//                   TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
//
//************************************************************************************
//
//  File:       T20600JD.java
//  Header:     none
//  Purpose:    Demonstrate basic Teradata SQL using the JDBC API by
//              creating/executing a stored procedure.
//              The program will:
//                -  Connect as user guest/please
//                -  Create a stored procedure
//                -  Execute the stored procedure
//                -  Obtain and display the results
//                -  Disconnect.
//
//  JDBC API: java.sql.Connection, java.sql.Statement,
//            java.sql.Statement.executeUpdate,
//            java.sql.CallableStatement,
//            java.sql.CallableStatement.setString,
//            java.sql.CallableStatement.registerOutParameter
//
//  Version: Updated for Teradata V2R6
//
//************************************************************************************

import java.sql.*;

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

        // Stored procedure creation command
        String sProcedure =
            "REPLACE PROCEDURE getDeptJobInfo " +
            "(IN name VARCHAR(30), OUT dept VARCHAR(50), OUT job VARCHAR(300)) " +
            "BEGIN " +
                "SELECT empDept, empJob INTO :dept, :job " +
                "FROM employee " +
                "WHERE empName = :name; " +
            "END;";
        String sCall = "{CALL getDeptJobInfo(?,?,?)}";
        String sName = "Brian Lee";
        try
        {
            System.out.println(" Sample T20600JD: ");
            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
                {
                    System.out.println(" Attempting to create a procedure : "
                                       + sProcedure);
                    // Sending the procedure creation DDL request to the
                    // database, replacing any old implementations, if present
                    stmt.executeUpdate(sProcedure);
                    // If no errors occured...
                    System.out.println(" Procedure created successfully.");

                    // The CallableStatement object represents a precompiled
                    // SQL statement. It provides methods for setting up its
                    // IN and OUT parameters, and methods for executing the
                    // call to a stored procedure. Please refer to the JAVA API
                    // or the supporting driver documentation for a complete
                    // list of methods, their implementations, and
                    // return values.
                    // Creating a CallableStatement object for calling the
                    // database stored procedure and preparing the callable
                    // statement for execution
                    CallableStatement cStmt = con.prepareCall(sCall);
                    // Setting up input parameter value
                    cStmt.setString(1, sName);
                    // Setting up output parameters for data retrieval by
                    // declaring parameter types.
                    cStmt.registerOutParameter(2, Types.VARCHAR);
                    cStmt.registerOutParameter(3, Types.VARCHAR);
                    System.out.println(" Calling the procedure with '"
                                       + sName + "' ...");
                    // Making a procedure call
                    cStmt.executeUpdate();
                    // Displaying procedure call result, please refer to the
                    // driver manual for a full list of data retrieval methods
                    System.out.println(" Call successful.");
                    System.out.println(" Displaying output of the call to "
                                       + "getDeptJobInfo(...) : ");
                    System.out.println(" " + sName);
                    System.out.println(" -----------------");
                    System.out.println(" Department : " + cStmt.getString(2));
                    System.out.println(" Job Description : " + cStmt.getString(3));
                }
                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 T20600JD 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 T20600JD



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