T21010JD.java





T21010JD.java Expand / Collapse
//************************************************************************************
//
//                 Copyright (c) 2004-2008 by Teradata Corporation
//                         All Rights Reserved
//
//                   TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
//
//************************************************************************************
//
//  File:       T21010JD.java
//  Header:     none
//  Purpose:    Demonstrate searching for specific procedure column
//              names and searching for column names for a
//              specific procedure by using wildcard characters
//              The program will:
//                -  Connect as user guest/please
//                -  Obtain and display names of all procedure
//                   parameter and result columns in all databases
//                   matching a specified name pattern
//                -  Obtain and display names of all procedure
//                   parameter and result columns in all databases
//                   for a specific procedure using wildcard characters
//                -  Disconnect.
//
//  JDBC API: java.sql.Connection, java.sql.DatabaseMetaData,
//            java.sql.DatabaseMetaData.getProcedureColumns
//
//  Version: Updated for Teradata V2R6
//
//************************************************************************************

import java.sql.*;

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

        // Wildcard name patterns used for procedure column search
        String colWildName = "i_";
        String procWildName = "get%Info";

        try
        {
            System.out.println(" Sample T21010JD: ");
            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 DatabaseMetaData object from an active
                // connection.
                DatabaseMetaData dbmd = con.getMetaData();
                System.out.println(" DatabaseMetaData object created. ");

                // The following code demonstrates searching for
                // procedure columns in all databases.

                // Use getProcedureColumns to generate a result set
                // of procedure columns matching a specified name pattern.
                ResultSet rs = dbmd.getProcedureColumns(null, null,
                                                        null, colWildName);
                // Display the procedure column names
                System.out.println(" DISPLAYING ALL PROCEDURE COLUMNS"+
                " IN ALL DATABASES MATCHING \"" + colWildName + "\":");
                System.out.println(" Database Name : Procedure Name :" +
                                   " Column Name : Column Type");
                System.out.println(" --------------------------------" +
                                   "--------------------------");
                while(rs.next())
                {
                    System.out.println(" " + rs.getString("PROCEDURE_SCHEM")
                    + " : " + rs.getString("PROCEDURE_NAME")
                    + " : " + rs.getString("COLUMN_NAME")
                    + " : " + rs.getString("COLUMN_TYPE"));
                }


                // Search for all parameter and result columns belonging
                // to a specified procedure matching a specified name
                // pattern. Use getProcedureColumns to generate
                // a result set of procedure columns.
                rs = dbmd.getProcedureColumns(null, null,
                                              procWildName, null);
                // Display the procedure column names
                System.out.println(" DISPLAYING ALL PROCEDURE COLUMNS"+
                " OF PROCEDURES MATCHING \"" + procWildName + "\":");
                System.out.println(" Database Name : Procedure Name :" +
                                   " Column Name : Column Type");
                System.out.println(" --------------------------------" +
                                   "--------------------------");
                while(rs.next())
                {
                    System.out.println(" " + rs.getString("PROCEDURE_SCHEM")
                    + " : " + rs.getString("PROCEDURE_NAME")
                    + " : " + rs.getString("COLUMN_NAME")
                    + " : " + rs.getString("COLUMN_TYPE"));
                }
            }
            finally
            {
                // Close the connection
                System.out.println(" Closing connection to Teradata...");
                con.close();
                System.out.println(" Connection to Teradata closed. ");
            }

            System.out.println(" Sample T21010JD 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 T21010JD



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