//************************************************************************************ // // Copyright (c) 2006-2008 by Teradata Corporation // All Rights Reserved // // TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET // //************************************************************************************ // // File: T20003JD.java // Header: none // Purpose: Demonstrate how to set up global temporary table, which is // required for lob updates. Used by T20404 to update lob. // The program will: // - Connect as user guest/please // - Drop global temporary table JdbcLobUpdate if it exists // - Create global temporary table JdbcLobUpdate with the // following columns: // id INTEGER NOT NULL, // bval BLOB, // cval CLOB CHARACTER SET UNICODE // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.Statement, // java.sql.Statement.executeUpdate // // Version: Updated for Teradata 12 // //************************************************************************************
import java.sql.*;
public class T20003JD { // 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";
// Statements used in table creation String sDropTbl = "DROP TABLE JdbcLobUpdate"; String sCreateTbl = " CREATE GLOBAL TEMPORARY TABLE JdbcLobUpdate("+ " id INTEGER NOT NULL,"+ " bval BLOB,"+ " cval CLOB CHARACTER SET UNICODE)"+ " UNIQUE PRIMARY INDEX upi_JdbcLobUpdate(id)"+ " ON COMMIT PRESERVE ROWS";
try { System.out.println(" Sample T20003JD: "); 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. ");
// 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. ");
try { // Creating a statement object from an active connection. Statement stmt = con.createStatement(); System.out.println(" Statement object created. ");
try { // Cleanup procedure: // If the sample table already exists, drop it. try { System.out.println(" Dropping table if present: " + sDropTbl); stmt.executeUpdate(sDropTbl); System.out.println(" Table dropped."); } catch (SQLException ex) { // If the table did not exist, no drop is required. // Ignore the raised "no table present" exception by // printing out the error message and swallowing the // exception. System.out.println(" Drop table exception ignored: " + ex); System.out.println(" Table could not be dropped." + " Execution will continue..."); }
// Create the global temporary table System.out.println(" Creating table: " + sCreateTbl); stmt.executeUpdate(sCreateTbl); System.out.println(" Sample table created. "); } finally { // Close the statement stmt.close(); System.out.println(" Statement object closed. "); } } finally { // Close the connection System.out.println(" Closing connection to Teradata..."); con.close(); System.out.println(" Connection to Teradata closed. "); }
System.out.println(" Sample T20003JD finished. "); } 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 T20003JD
|
|
|
|