//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T21202JD.java // Header: none // Purpose: Demonstrate how to use the com.sun.rowset.JdbcRowSetImpl // (java.sql.ResultSet) class with the Teradata JDBC // Driver. This program can be compiled with JDK 1.4, but it // requires JDK 1.5 (J2SE 5.0) to run. // The program will: // - Connect as user guest/please to sample database // - Create a RowSet using java.sql.ResultSet // - Retrieve values from RowSet and display the results // - Disconnect // // JDBC API: java.sql.Connection, // javax.sql.RowSet // //********************************************************************* import java.sql.*; import javax.sql.*; import java.lang.reflect.*; public class T21202JD { public static void main (String [] args) throws ClassNotFoundException , IllegalAccessException , InvocationTargetException , InstantiationException , NoSuchMethodException { try { String url = "jdbc:teradata://whomooz/TMODE=ANSI,CHARSET=UTF8," + "COMPAT_DBS=TRUE,COMPAT_ISAUTOINC=TRUE,COMPAT_ISCURRENCY=TRUE," + "COMPAT_ISSIGNED=TRUE,COMPAT_ISSEARCH=TRUE," + "COMPAT_GETSCHEMA=SCHEMA,COMPAT_GETTABLE=TABLE"; String sUser = "guest"; String sPassword = "please"; String sqlSelect = "SELECT * FROM employee"; System.out.println("\n Sample T21202JD: \n"); System.out.println(" Looking for 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."); try { System.out.println(" Connection to Teradata established. \n"); Statement stmt = con.createStatement () ; try { System.out.println(" Executing query"); ResultSet rs = stmt.executeQuery (sqlSelect) ; System.out.println(" Done.\n"); // The use of reflection rather than a hardcoded reference to // "new JdbcRowSetImpl" allows this program to compile on JDK 1.4 Class cls = Class.forName("com.sun.rowset.JdbcRowSetImpl"); Constructor cn = cls.getConstructor(new Class[] { ResultSet.class }); System.out.println(" Creating JdbcRowSetImpl"); RowSet jrs = (RowSet) cn.newInstance(new Object[] { rs }); try { System.out.println(" JdbcRowSetImpl created.\n"); System.out.println(" Rows:"); while (jrs.next ()) { int empID = jrs.getInt(1); String empName = jrs.getString(2); String empDept = jrs.getString(3); String empJob = jrs.getString(4); System.out.println(" " + empID + ", " + empName + ", " + empDept + ", " + empJob); } System.out.println(); } finally { System.out.println(" Closing JdbcRowSetImpl"); jrs.close(); System.out.println(" JdbcRowSetImpl closed.\n"); } } finally { System.out.println(" Closing Statement"); stmt.close () ; System.out.println(" Statement 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 T21202JD 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 }