//************************************************************************************ // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // // TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET // //************************************************************************************ // // File: T21009JD.java // Header: none // Purpose: Demonstrate searching for specific procedure column // names and searching for column names for a // specific procedure by name // The program will: // - Connect as user guest/please // - Obtain and display names of all procedure // parameter and result columns in all databases // matching a specified name // - Obtain and display names of all procedure // parameter and result columns in all databases // for a specific procedure // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.DatabaseMetaData, // java.sql.DatabaseMetaData.getProcedureColumns // // Version: Updated for Teradata V2R6 // //************************************************************************************
import java.sql.*;
public class T21009JD { // 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";
// Names used for procedure column search String colName = "job"; String procName = "getEmpInfo";
try { System.out.println(" Sample T21009JD: "); 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 DatabaseMetaData object from an active // connection. DatabaseMetaData dbmd = con.getMetaData(); System.out.println(" DatabaseMetaData object created. ");
// The following code demonstrates searching for // procedure columns in all databases.
// Use getProcedureColumns to generate a result set // of procedure columns matching a specified name. ResultSet rs = dbmd.getProcedureColumns(null, null, null, colName); // Display the procedure column names System.out.println(" DISPLAYING ALL PROCEDURE COLUMNS"+ " IN ALL DATABASES MATCHING \"" + colName + "\":"); System.out.println(" Database Name : Procedure Name :" + " Column Name : Column Type"); System.out.println(" --------------------------------" + "--------------------------"); while(rs.next()) { System.out.println(" " + rs.getString("PROCEDURE_SCHEM") + " : " + rs.getString("PROCEDURE_NAME") + " : " + rs.getString("COLUMN_NAME") + " : " + rs.getString("COLUMN_TYPE")); }
// Search for all parameter and result columns belonging // to a specified procedure. Use getProcedureColumns to // generate a result set of procedure columns. rs = dbmd.getProcedureColumns(null, null, procName, null); // Display the procedure column names System.out.println(" DISPLAYING ALL PROCEDURE COLUMNS"+ " OF PROCEDURES MATCHING \"" + procName + "\":"); System.out.println(" Database Name : Procedure Name :" + " Column Name : Column Type"); System.out.println(" --------------------------------" + "--------------------------"); while(rs.next()) { System.out.println(" " + rs.getString("PROCEDURE_SCHEM") + " : " + rs.getString("PROCEDURE_NAME") + " : " + rs.getString("COLUMN_NAME") + " : " + rs.getString("COLUMN_TYPE")); } } finally { // Close the connection System.out.println(" Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. "); }
System.out.println(" Sample T21009JD 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 T21009JD
|
|
|
|