//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T21201JD.java // Header: none // Purpose: Demonstrate how to use the com.sun.rowset.JdbcRowSetImpl // (java.sql.Connection) 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 // - Insert a row to sample table employee // - Create a RowSet using java.sql.connection // - Retrieve values from RowSet and display the results // - Disconnect // // JDBC API: java.sql.Connection, // java.sql.Statement, // java.sql.Statement.executeUpdate, // javax.sql.RowSet // //********************************************************************* import java.sql.*; import javax.sql.*; import java.lang.reflect.*; public class T21201JD { public static void main (String [] args) throws ClassNotFoundException , IllegalAccessException , InvocationTargetException , InstantiationException , NoSuchMethodException { try { String url = "jdbc:teradata://whomooz/TMODE=ANSI,CHARSET=UTF8"; String sUser = "guest"; String sPassword = "please"; String sqlSelect = "SELECT * FROM employee"; System.out.println("\n Sample T21201JD: \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); try { System.out.println(" User " + sUser + " connected."); System.out.println(" Connection to Teradata established. \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[] { Connection.class }); System.out.println(" Creating JdbcRowSetImpl"); RowSet jrs = (RowSet)cn.newInstance(new Object[] { con }); try { System.out.println(" JdbcRowSetImpl created.\n"); jrs.setCommand(sqlSelect); System.out.println(" Executing query"); jrs.execute(); System.out.println(" Done.\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 { // Close the connection System.out.println(" Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. \n"); } System.out.println(" Sample T21201JD 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 }