T20606JD.java





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

import java.sql.*;

public class T20606JD
{
    // 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 getDeptEmployees"
                    +"(IN empDept VARCHAR(30)) "
                    +"DYNAMIC RESULT SETS 1 "
                    +"BEGIN "
                    +"     DECLARE cur1 CURSOR WITH RETURN ONLY FOR "
                    +"        SELECT empID, empName, empJob FROM employee2"
                    +"           WHERE empDept = :empDept ORDER BY empID; "
                    +"     OPEN cur1; "
                    +"END;";
        String sCall = "{CALL getDeptEmployees(?)}";
        String sDept = "Human Resources";
        try
        {
            System.out.println(" Sample T20606JD: ");
            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 : ");
                    System.out.println(" "+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 input parameter value
                    cStmt.setString(1, sDept);
                    System.out.println(" Calling the procedure with '"
                                       + sDept + "' ...");
                    // Making a procedure call, creating a result set object
                    ResultSet rs = cStmt.executeQuery();
                    // Displaying procedure call result, please refer to the
                    // driver manual for a full list of data retrieval methods
                    System.out.println(" Call successful.");
                    // Extract and display dynamic result set data
                    System.out.println();
                    System.out.print(" DISPLAYING EMPLOYEES FOR DEPARTMENT: ");
                    System.out.println(sDept);
                    System.out.print(" --------------------------");
                    System.out.println("--------------------------");

                    int rowCount = 0;   // result set row counter
                    int numData;        // placeholder for integer data
                    String charData;    // placeholder for character data

                    // iterate through all returned rows and display them
                    while (rs.next())
                    {
                        rowCount++;   // increment retrieved row counter
                        System.out.println();
                        // retrieve and display first column value
                        numData = rs.getInt(1);
                        System.out.println(" Employee ID: " + numData);
                        // retrieve and display second column value
                        charData = rs.getString(2);
                        System.out.println(" Name: " + charData);
                        // retrieve and display third column value
                        charData = rs.getString(3);
                        System.out.println(" Job Description: " + charData);
                   }
                   System.out.println(" " + rowCount + " Row(s) returned.");
                }
                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 T20606JD 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 T20606JD



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