//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20401JD.java // Header: none // Purpose: Demonstrate basic Teradata SQL using the JDBC API by // executing an UPDATE statement using parameter markers. // The program will: // - Connect as user guest/please // - Update a row in the employee table // - Display the number of rows updated // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.PreparedStatement, // java.sql.PreparedStatement.executeUpdate // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T20401JD { // 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"; // Creating strings representing a prepared statement and its // respective parameter values String sUpdate = "UPDATE employee SET empJob = ? WHERE empID = ?"; int Id = 100002; String Description = "Manager overseeing international contracts"; try { System.out.println("\n Sample T20401JD: \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 { System.out.println(" Preparing this SQL statement for" + " execution:\n " + sUpdate); // Creating a prepared statement object from an active connection PreparedStatement pstmt = con.prepareStatement(sUpdate); System.out.println(" Prepared statement object created. \n"); try { int RowCount; // Return value for updated row count // The following code will perform an UPDATE to the table // using a prepared statement and display the number of // rows updated. System.out.println(" Attempting an update...\n"); // Set parameter values indicated by ? (dynamic update) System.out.println(" Using setInt() and setString() to " + "bind values to parameter markers:\n"); pstmt.setString(1, Description); System.out.println(" FIRST ? set to: " + Description); pstmt.setInt(2, Id); System.out.println(" SECOND ? set to: " + Id); // Call without parameter to execute SQL command RowCount = pstmt.executeUpdate(); System.out.println("\n Update completed successfully: " + RowCount + " row(s) updated."); } finally { // Close the statement pstmt.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 T20401JD 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 T20401JD