//********************************************************************* // // Copyright (c) 2004-2008 by Teradata Corporation // All Rights Reserved // //********************************************************************* // // File: T21101JD.java // Header: none // Purpose: Demonstrate retrieval and modification of statement // options // The program will: // - Connect as user guest/please // - Obtain, display, and change following statement // options: // - MaxFieldSize // - MaxRows // - QueryTimeout // - Disconnect. // // JDBC API: java.sql.Connection, java.sql.Statement, // java.sql.Statement.getMaxFieldSize, // java.sql.Statement.setMaxFieldSize, // java.sql.Statement.getMaxRows, // java.sql.Statement.setMaxRows, // java.sql.Statement.getQueryTimeout, // java.sql.Statement.setQueryTimeout // // Version: Updated for Teradata V2R6 // //********************************************************************* import java.sql.*; public class T21101JD { // 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"; try { System.out.println("\n Sample T21101JD: \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 { // The following code will get and set statement options. // Only getMaxFieldSize/setMaxFieldSize, // getMaxRows/setMaxRows, getQueryTimeout/setQueryTimeout // methods will be demonstrated in this example. // Please refer to the supporting driver documentation // for a full list of supported methods, their // implementations, and their return values. System.out.println(" Retrieving and modifying maximum field" + " size setting :\n"); // getMaxFieldSize method retrieves the maximum number of // bytes that can be returned for character and binary // column in a ResultSet produced by the current Statement // object. This limit applies to BINARY, VARBINARY, // LONGVARBINARY, CHAR, VARCHAR, and LONGVARCHAR columns. // If the limit is exceeded, the excess data is silently // discarded. The method returns the current limit in bytes // or zero if there is no limit. // setMaxFieldSize method sets the above limit to a // specified number of bytes. The parameter is the new // column size limit in bytes or zero for no limit. // Retrieve current/default value of the maximum field size. int maxSize = stmt.getMaxFieldSize(); System.out.println(" Default (current) maximum field size" + " value : " + maxSize); // Change the default size value maxSize = 10000; System.out.println(" Setting the size value to : " + maxSize); stmt.setMaxFieldSize(maxSize); // Retrieve and display the new maximum size value System.out.println(" New maximum field size value : " + stmt.getMaxFieldSize()); System.out.println("\n Retrieving and modifying maximum" + " number of rows setting :\n"); // getMaxRows method retrieves the maximum number of rows // that a ResultSet object produced by the current Statement // object can contain. If the limit is exceeded, the excess // rows are silently dropped. The method returns the current // maximum number of rows or zero for no limit. // setMaxRows method sets the above limit to a specified // number. The parameter is the new maximum number of rows // or zero for no limit. // Retrieve current/default value of the maximum field size. int maxRows = stmt.getMaxRows(); System.out.println(" Default (current) maximum row number" + " value : " + maxRows); // Change the default value maxRows = 100000; System.out.println(" Setting the maximum number of rows " + "to : " + maxRows); stmt.setMaxRows(maxRows); // Retrieve and display the new value System.out.println(" New maximum number of rows value: " + + stmt.getMaxRows()); System.out.println("\n Retrieving and modifying query" + " timeout setting :\n"); // getQueryTimeout method retrieves the number of seconds // the driver will wait for a for a Statement to execute. // If the limit is exceeded, a SQLException is thrown. // setQueryTimeout method sets the above limit to a // specified number of seconds. The parameter is the // the new query timeout limit in seconds or zero for an // unlimited number of seconds. // Retrieve the current/default value of the query timeout // setting. int queryTime = stmt.getQueryTimeout(); System.out.println(" Default (current) query timeout" + " value : " + queryTime); // Change the default value queryTime = 300; System.out.println(" Setting the query timeout to : " + queryTime); stmt.setQueryTimeout(queryTime); // Retrieve and display the new value System.out.println(" New query timeout value: " + + stmt.getQueryTimeout()); } finally { // Close the statement stmt.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 T21101JD 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 T21101JD