//********************************************************************* // // Copyright (c) 2006-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20604JD.java // Header: none // Purpose: Demonstrate basic Teradata SQL using the JDBC API by // creating/executing a Java Stored Procedure. // The program will: // - Connect as user guest/please // - Create a stored procedure // - Execute the stored procedure // - Obtain and display the results // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.Statement, // java.sql.Statement.executeUpdate, // java.sql.CallableStatement, // java.sql.CallableStatement.setString, // java.sql.CallableStatement.registerOutParameter // // Version: Created for Teradata Database 12 // //********************************************************************* import java.sql.*; public class T20604JD { // 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"; //Create a Java Stored Procedure creation command String sProcedure = "REPLACE PROCEDURE getDeptJobInfo " + "(IN name VARCHAR(30), OUT dept VARCHAR(50), OUT job VARCHAR(300)) " + "LANGUAGE JAVA MODIFIES SQL DATA " + "PARAMETER STYLE JAVA " + "EXTERNAL NAME" + " 'SampleJXSP:com.teradata.sample.DeptJobInfo.getDeptJobInfo'"; String sCall = "{CALL getDeptJobInfo(?,?,?)}"; String sName = "Brian Lee"; try { System.out.println("\n Sample T20604JD: \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 { System.out.println(" Attempting to create a procedure : " + sProcedure); // Sending the procedure creation DDL request to the // database, replacing any old implementations, if present stmt.executeUpdate(sProcedure); // If no errors occured... System.out.println(" Procedure created successfully."); // The CallableStatement object represents a precompiled // SQL statement. It provides methods for setting up its // IN and OUT parameters, and methods for executing the // call to a stored procedure. Please refer to the JAVA API // or the supporting driver documentation for a complete // list of methods, their implementations, and // return values. // Creating a CallableStatement object for calling the // database stored procedure and preparing the callable // statement for execution CallableStatement cStmt = con.prepareCall(sCall); // Setting up input parameter value cStmt.setString(1, sName); // Setting up output parameters for data retrieval by // declaring parameter types. cStmt.registerOutParameter(2, Types.VARCHAR); cStmt.registerOutParameter(3, Types.VARCHAR); System.out.println("\n Calling the procedure with '" + sName + "' ..."); // Making a procedure call cStmt.executeUpdate(); // Displaying procedure call result, please refer to the // driver manual for a full list of data retrieval methods System.out.println(" Call successful."); System.out.println("\n Displaying output of the call to " + "getDeptJobInfo(...) : "); System.out.println("\n " + sName); System.out.println(" -----------------"); System.out.println(" Department : " + cStmt.getString(2)); System.out.println(" Job Description : " + cStmt.getString(3)); } 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 T20604JD 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 T20604JD