//
// Source File Name: Prepare.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.
//
// This sample program shows how prepare then repeatedly execute a SQL
// statement in Java/
// 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 Prepare {
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 {
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();
}
displayDivisionInfo (con);
System.out.println ("\n>Disconnecting...");
con.close();
} catch (Exception e) { e.printStackTrace(); }
}
//
// displayDivisionInfo
// - executes a prepared statement that displays info for user
// - define division
//
public static void displayDivisionInfo (Connection con) {
try {
System.out.println ("\nEnter Division Name or 'q' to quit:");
System.out.println ("(Eastern, Western, Midwest, Corporate)");
String division = Tools.readString();
PreparedStatement pstmt = con.prepareStatement("SELECT deptname, location " +
"from org where division = ? ");
String dept = new String("");
String loc = new String("");
while (!(division.equalsIgnoreCase ("q"))) {
pstmt.setString (1, division);
ResultSet rs = pstmt.executeQuery();
System.out.println ("\nDepartments in " + division + " division:");
System.out.println ("DEPTNAME\t Location");
System.out.println ("---------------- --------------");
// display the result set
// rs.next() returns false when there are no more rows
while (rs.next()) {
dept = rs.getString (1);
loc = rs.getString (2);
System.out.println (Tools.padLength(dept, 17) + loc);
}
System.out.println ("\nEnter Division Name or 'q' to quit:");
System.out.println ("(Eastern, Western, Midwest, Corporate)");
division = Tools.readString();
rs.close();
}
pstmt.close();
} catch (Exception e) { e.printStackTrace(); }
}
}