//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T20200JD.java // Header: none // Purpose: Demonstrate processing of basic INSERT statements // without using parameter markers. // The program will: // - Connect as user guest/please // - Perform a series of insertions 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 T20200JD { // 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"; // Preparing various insertion commands String[] sInsert = {"INSERT INTO employee VALUES(100001, 'Mike Smith'," + " 'Product Development', 'QA engineer responsible for planning," + " developing and maintaining frameworks for test automation')", "INSERT INTO employee VALUES(100002, 'James Parker'," + " 'Marketing', 'Manager coordinating international sales')", "INSERT INTO employee VALUES(100003, 'Mary Jones'," + " 'Product Development', 'Software engineer responsible for" + " regression test plans')", "INSERT INTO employee VALUES(100004, 'John Walker'," + " 'Human Resources', 'Manager responsible for employee" + " benefits programs')", "INSERT INTO employee VALUES(100005, 'Steven Brown'," + " 'Customer Service', 'Software engineer providing technical" + " applications support')", "INSERT INTO employee VALUES(100006, 'Susan Young'," + " 'Product Development', 'QA engineer overlooking overseas testing')", "INSERT INTO employee VALUES(100007, 'Brian Lee', 'Marketing'," + " 'Team leader managing market research')"}; try { System.out.println("\n Sample T20200JD: \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 { int RowCount; // Return value for row count // The following code will demonstrate performing a series // of insertions into the table using the SQL INSERT // statements. Numerous inserts are performed here to aid in // subsequent examples. // Retrieve the number of insertions to be performed int len = sInsert.length; // Perform insertions and display row counts as returned // by the Statement.executeUpdate method. for (int i = 0; i < len; i++) { System.out.println(" Attempting an insertion:\n " + sInsert[i]); RowCount = stmt.executeUpdate(sInsert[i]); 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 T20200JD 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 T20200JD