//********************************************************************* // // Copyright (c) 2006-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20911JD.java // Header: none // Purpose: Demonstrate obtaining optimal set of row-identifying columns // The program will: // - Connect as user guest/please // - Obtain and display names of optimal set of row- // identifying columns in a table in the target database // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.DatabaseMetaData, // java.sql.DatabaseMetaData.getBestRowIdentifer // // Version: Updated for Teradata V2R6.1 // //********************************************************************* import java.sql.*; public class T20911JD { // 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"; // Table name for retrieving optimal set of row-identifying columns String tableName = "employee"; // scope - the scope of interest int scope = DatabaseMetaData.bestRowSession; try { System.out.println("\n Sample T20911JD: \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 obtains the names of the optimal set of // row-identifying columns in a table in the database. // Use getBestRowIdentifer method to generate a result set of // row-identifying column names. // This method has the following parameters: // parameter 1: a String object representing a catalog name // parameter 2: a String object representing a schema name // (database name) // parameter 3: a String object representing a table name // (table name) // parameter 4: an int value representing the scope of interest // The scope value can be the following values: // bestRowTemporary - very temporary, while using row // bestRowTransaction - valid for remainder of current transaction // bestRowSession - valid for remainder of current session // parameter 5: a boolean value indicating if nullable columns // are included // Each column description includes the following columns: // SCOPE (short) // COLUMN_NAME (String) // DATA_TYPE (int) // TYPE_NAME (String) // COLUMN_SIZE (int) // BUFFER_LENGTH (int) // DECIMAL_DIGITS (short) // PSEUDO_COLUMN (short) // Please refer to the driver manual for a complete // description of this method, its implementations, // and return values. // The call below will obtain optimal set of row-identifying // columns in the table "employee" in the "guest" database ResultSet rs = dbmd.getBestRowIdentifier(null, "guest", tableName, scope, true); // Display the retrieved column names System.out.println(" Displaying the column names" + " in the employee table in the guest database:\n"); System.out.println(" Column Name : Data Type :" + " Type Name"); System.out.println(" -------------------------" + " ----------"); while(rs.next()) { System.out.println(" " + rs.getString("COLUMN_NAME") + " : " + rs.getInt("DATA_TYPE") + " : " + 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 T20911JD 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 T20911JD