//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20902JD.java // Header: none // Purpose: Demonstrate obtaining a list of supported SQL data types // The program will: // - Connect as user guest/please // - Obtain and display a list of all SQL data types // supported by the database // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.DatabaseMetaData, // java.sql.DatabaseMetaData.getTypeInfo // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T20902JD { // 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"; try { System.out.println("\n Sample T20902JD: \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 DatabaseMetaData object from an active // connection. DatabaseMetaData dbmd = con.getMetaData(); System.out.println(" DatabaseMetaData object created. \n"); // The following code will obtain a description of all // SQL datatypes supported by this database // Retrieve supported types descriptions ResultSet rs = dbmd.getTypeInfo(); // Results are ordered by DATA_TYPE and then by how closely // the data type maps to the corresponding JDBC SQL type. // Each type description has the following 18 columns: // TYPE_NAME (String) // DATA_TYPE (short) // PRECISION (int) // LITERAL_PREFIX (String) // LITERAL_SUFFIX (String) // CREATE_PARAMS (String) // NULLABLE (short) // CASE_SENSITIVE (boolean) // SEARCHABLE (short) // UNSIGNED_ATTRIBUTE (boolean) // FIXED_PREC_SCALE (boolean) // AUTO_INCREMENT (boolean) // LOCAL_TYPE_NAME (String) // MINIMUM_SCALE (short) // MAXIMUM_SCALE (short) // SQL_DATA_TYPE (int) // SQL_DATETIME_SUB (int) // NUM_PREC_RADIX (int) // Please refer to the driver manual for a complete // description of methods, their implementations, and return // values. // Only the name of the supported types will be displayed, // all other available information can be retrieved in a // similar manner using the ResultSet data retrieval methods. // Display the supported types System.out.println(" DISPLAYING SUPPORTED SQL DATA TYPES:\n"); while(rs.next()) { System.out.println(" " + rs.getString("TYPE_NAME")); } } finally { // Close the connection System.out.println("\n Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. \n"); } System.out.println(" Sample T20902JD 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 T20902JD