//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20202JD.java // Header: none // Purpose: Demonstrate insertion of LOB values without using // parameter markers. // The program will: // - Connect as user guest/please // - Perform an insertion into the table // - Disconnect. // // JDBC API: java.sql.Connection, // java.sql.Statement, java.sql.Statement.executeUpdate // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T20202JD { // 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"; // String representing a SQL INSERT statement with LOB parameters. // Please note that there are two ways of inserting LOB values: // using binary/character input streams and using SQL binary/character // literals. Please refer to the Teradata SQL manual for the complete // description of SQL literals. // For the CLOB value, a character string will be used. For the BLOB // value, a hexadecimal literal will be used, representing binary data. // Once the BLOB/CLOB values are in the correct format, the statement // is executed using the Statement.executeUpdate method. // Please note that the empRawPhotoData is set in this case to // denote the insertion of BLOB data using hexadecimal literals. String sInsert = "INSERT INTO employee2 VALUES (100001, 'Mike Smith'," + " 'Product Development', 'QA engineer responsible for planning," + " developing and maintaining frameworks for test automation'," + " 'Resume of Mike Smith.'," + " '800FA01004480000D8FFE00FDB0040104A464946000101104A46FFDB'XB, 1)"; try { System.out.println("\n Sample T20202JD: \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 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"); // Perform an insertion into the table try { int RowCount; // Return value for row count // The following code will perform an INSERT into the table // and display the update count. System.out.println(" Attempting an insertion:\n " + sInsert); RowCount = stmt.executeUpdate(sInsert); System.out.println(" Insertion completed successfully: " + RowCount + " row(s) inserted.\n"); } finally { // Close the statement stmt.close(); System.out.println(" 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 T20202JD 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 T20202JD