//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20101JD.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 T20101JD { // 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, // column metadata can be retrieved. String sSelAll = " SELECT * FROM employee2"; try { System.out.println("\n Sample T20101JD: \n"); 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. \n"); // 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. \n"); try { // Creating a statement object from an active connection Statement stmt = con.createStatement(); System.out.println(" Statement object created. \n"); try { // Create a ResultSet object representing the data // resulting from the query ResultSet rset = stmt.executeQuery(sSelAll); // Create a ResultSetMetaData object that allows // meta data information retrieval about the query result ResultSetMetaData rsmd = rset.getMetaData(); // Retrieve the number of columns returned int colCount = rsmd.getColumnCount(); System.out.println(" This table has " + colCount + " columns.\n"); // 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 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("\n Statement object closed. \n"); } } finally { // Close the connection System.out.println(" Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. \n"); } System.out.println(" Sample T20101JD finished. \n"); } 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 T20101JD