//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20910JD.java // Header: none // Purpose: Demonstrate obtaining description of a table's // primary key columns. // The program will: // - Connect as user guest/please // - Obtain and display a list of table's primary // key columns // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.DatabaseMetaData, // java.sql.DatabaseMetaData.getPrimaryKeys // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T20910JD { // 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 to be used in primary key columns retrieval String tableName = "employee"; try { System.out.println("\n Sample T20910JD: \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 list of primary // key columns for a specified table // Use getProcedureColumns to generate a result set // of primary key columns. This method has the following // parameters: // parameter 1: a String object representing a catalog name // parameter 2: a String object representing a schema name // pattern (database name) // parameter 3: a String object representing a table // name pattern (table name) // Each primary key column description will contain // the following: // TABLE_CAT (String) // TABLE_SCHEM (String) // TABLE_NAME (String) // COLUMN_NAME (String) // KEY_SEQ (short) // PK_NAME (String) // Results will be ordered by COLUMN_NAME. Please refer to // the driver manual for a complete description of this // method, its implementations, and return values. // The call below will retrieve all primary key columns // for the specified table. ResultSet rs = dbmd.getPrimaryKeys(null, null, tableName); // Display the primary key columns System.out.println(" DISPLAYING ALL PRIMARY KEY COLUMNS"+ " FOR A TABLE MATCHING \"" + tableName + "\":\n"); System.out.println(" Database Name : Table Name :" + " Column Name : Sequence Within Key : Key Name"); System.out.println(" ----------------------------" + "---------------------------------------------"); while(rs.next()) { System.out.println(" " + rs.getString("TABLE_SCHEM") + " : " + rs.getString("TABLE_NAME") + " : " + rs.getString("COLUMN_NAME") + " : " + rs.getShort("KEY_SEQ") + " : " + rs.getString("PK_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 T20910JD 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 T20910JD