// // Source File Name: TypeInfo.java 1.3 // // Licensed Materials -- Property of IBM // // (c) Copyright International Business Machines Corporation, 1999. // All Rights Reserved. // // US Government Users Restricted Rights - // Use, duplication or disclosure restricted by // GSA ADP Schedule Contract with IBM Corp. // // PURPOSE : // uses getTypeInfo() to retrieve SQL datatype info // For more information about this sample, refer to the README file. // For more information on Programming in Java, refer to the // "Programming in Java" section of the Application Development Guide. // For more information on building and running Java programs for DB2, // refer to the "Building Java Applets and Applications" section of the // Application Building Guide. // For more information on the SQL language, refer to the SQL Reference. import java.io.*; import java.lang.*; import java.sql.*; class TypeInfo { static { try { // register the driver with DriverManager // The newInstance() call is needed for the sample to work with // JDK 1.1.1 on OS/2, where the Class.forName() method does not // run the static initializer. For other JDKs, the newInstance // call can be omitted. Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance(); } catch (Exception e) { e.printStackTrace(); } } public static void main (String[] argv) { Connection con = null; try { // connect to the server of user's choice // see tools.class on tools.DBConnect method if (argv.length == 3) { // connect using command line arguments con = Tools.DBConnect(argv[0], argv[1], argv[2]); } else { // prompt user for database name, user ID, password con = Tools.DBConnect(); } con.setAutoCommit(false); getDisplayTypeInfo (con); // disconnect from the connection made System.out.println ("\n>Disconnecting..."); con.commit(); con.close(); } catch (Exception e) { e.printStackTrace(); } } // // getDisplayTypeInfo // - retrieves a result set, containing tables and schemas, into a vector // public static void getDisplayTypeInfo (Connection con) { try { DatabaseMetaData dmd = con.getMetaData(); // use getTypeInfo to retrieve information on data types in the database ResultSet rs = dmd.getTypeInfo(); System.out.println ("Datatype Datatype Precision Nullable Case"); System.out.println ("Typename (int) Sensitive"); System.out.println ("------------------------- -------- ---------- -------- ---------"); // variables to be used in loop through the result set String typeName = new String(""); String booleanPrint = new String(""); String dataType = new String(""); String precision = new String(""); short nullable = 0; boolean caseSensitive = false; while (rs.next()) { // get from result set and add spaces to format typeName = rs.getString (1); dataType = String.valueOf (rs.getShort (2)); precision = String.valueOf (rs.getInt (3)); nullable = rs.getShort (7); caseSensitive = rs.getBoolean (8); // if nullable = 1 then column values are nullable else not if (nullable == DatabaseMetaData.typeNullable) { booleanPrint = "TRUE "; } else { booleanPrint = "FALSE "; } // output column Type info to screen System.out.print (Tools.padLength(typeName, 26) + Tools.padLengthRight(dataType, 8) + " "); System.out.print (Tools.padLengthRight(precision, 10) + " " + booleanPrint); if (caseSensitive) { booleanPrint = "TRUE"; } else { booleanPrint = "FALSE"; } System.out.print (" " + booleanPrint + "\n"); } rs.close(); } catch (Exception e) { e.printStackTrace(); } } }