// // Source File Name: Simple.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 : // Executes a query and uses ResultSet.next() to retrieve the data // from that query. Prints this data as formatted output. // 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 Simple { 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); displayEasternDivision (con); // disconnect from the connection made System.out.println ("\n>Disconnecting..."); con.commit(); con.close(); } catch (Exception e) { e.printStackTrace(); } } // // displayEasternDivision // - lists departments and locations of the Eastern division // public static void displayEasternDivision (Connection con) { try { // statement to select the department names // and locations within the eastern division Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery ("SELECT deptname, location from org WHERE division = 'Eastern'"); String deptName = new String(""); String location = new String(""); System.out.println ("\nDepartments in Eastern division:"); System.out.println ("DEPTNAME Location"); System.out.println ("-------------- -------------"); // if there is another row of data then print that row // otherwise there is nothing left to print while (rs.next()) { // retrieve data from the current row deptName = rs.getString (1); location = rs.getString (2); // print information using formatted output System.out.println (Tools.padLength(deptName, 15) + location); } rs.close(); stmt.close(); } catch (Exception e) { e.printStackTrace(); } } }