T20100JD.java





T20100JD.java Expand / Collapse
//************************************************************************************
//
//                 Copyright (c) 2004-2008 by Teradata Corporation
//                         All Rights Reserved
//
//                   TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
//
//************************************************************************************
//
//  File:       T20100JD.java
//  Header:     none
//  Purpose:    Demonstrate the JDBC API for retrieving statement
//              result set meta data.
//              The program will:
//                -  Connect as user guest/please
//                -  Prepare an SQL statement
//                -  Obtain result set meta data
//                -  Disconnect.
//
//  JDBC API: java.sql.Connection,
//            java.sql.Statement, java.sql.Statement.executeQuery,
//            java.sql.ResultSet, java.sql.ResultSet.getMetaData,
//            java.sql.ResultSetMetaData
//
//  Version: Updated for Teradata V2R6
//
//************************************************************************************

import java.sql.*;

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

        // Query statement selecting all columns of the table. Using this query,
        // result set column metadata can be retrieved.
        String sSelAll = " SELECT * FROM employee";

        try
        {
            System.out.println(" Sample T20100JD: ");
            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
                {
                    // Performing the SELECT query using Statement.executeQuery
                    // method. This method executes a SQL statement that returns
                    // a single ResultSet. Only one ResultSet per Statement can
                    // be open at any point in time. If the reading of one
                    // ResultSet is interleaved with the reading of another,
                    // each must have been generated by different Statements.
                    // A ResultSet provides access to a table of data generated
                    // by executing a Statement. The table rows are retrieved
                    // in sequence. Within a row, its column values can be
                    // accessed in any order.
                    ResultSet rset = stmt.executeQuery(sSelAll);
                    // Retrieve the properties of the ResultSet object.
                    // A ResultSetMetaData object can be used to determine
                    // the types and properties of the columns in a ResultSet.
                    ResultSetMetaData rsmd = rset.getMetaData();

                    // Retrieve the number of columns returned
                    int colCount = rsmd.getColumnCount();
                    System.out.println(" This table has " + colCount + " columns.");

                    // For every column, display it's information.
                    System.out.println(" Displaying column information: ");

                    int i = 1;     // Initialize loop counter
                    while(i <= colCount)
                    {
                        // This code will demonstrate all available methods for
                        // retrieving column meta data.

                        System.out.println();
                        System.out.println(" Column " + i);
                        System.out.println(" ------------ ");
                        // Display the suggested column title for use in
                        // printouts and displays
                        System.out.println(" Column label: "
                                           + rsmd.getColumnLabel(i));
                        // Display the column name
                        System.out.println(" Column name: "
                                           + rsmd.getColumnName(i));
                        // Display the SQL type of a column.
                        System.out.println(" Column type: "
                                           + rsmd.getColumnType(i));
                        // Display the type name of a column
                        System.out.println(" Column type name: "
                                           + rsmd.getColumnTypeName(i));
                        // Display the class name of a column - DR89445
                        System.out.println(" Column class name: "
                                           + rsmd.getColumnClassName(i));
                        // Display information on whether NULL values are allowed
                        System.out.println(" NULLs allowed: "
                                           + rsmd.isNullable(i));
                        // Display the normal maximum width of a column in characters.
                        System.out.println(" Maximum character width: "
                                           + rsmd.getColumnDisplaySize(i));
                        // Display precision: the number of decimal digits
                        // Note: default value is 0.
                        System.out.println(" Column precision"
                                           + " (number of decimal places): "
                                           + rsmd.getPrecision(i));
                        // Display the number of digits to the right of the
                        // decimal point. Note: default value is 0.
                        System.out.println(" Precision to the right of"
                                           + " the decimal point: "
                                           + rsmd.getScale(i));
                        // Increment column counter
                        i++;
                    }
                }
                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 T20100JD 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 T20100JD



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