// // Source File Name: UseThrds.java 1.8 // // Licensed Materials -- Property of IBM // // (c) Copyright International Business Machines Corporation, 1999,2000. // All Rights Reserved. // // US Government Users Restricted Rights - // Use, duplication or disclosure restricted by // GSA ADP Schedule Contract with IBM Corp. // // PURPOSE : // - demonstrates the use of threads to run asynchronous functions // - shows how to use a class to exchange information between thread // and main program // Notes: // (1): The "connect to database" method uses default user ID and // password to connect to the sample database // (2): This sample will not work on platforms for which DB2 does // not support multi-threaded applications. // 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 UseThrds { 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) { try { int numInt = 0; // Set the department number if (argv.length == 0) { numInt = 10; } else if (argv.length == 1) { String num = argv[0]; Integer temp = Integer.valueOf (num); numInt = temp.intValue(); } else { throw new Exception("\nUsage: java UseThrds [departmentNumber]\n"); } Department deprtmnt = new Department (numInt); // create and start the thread SQLExecution sqlexec = new SQLExecution (deprtmnt); sqlexec.start(); // indicate that thread is running along with the program System.out.print ("\nRunning thread..."); while (sqlexec.isAlive()) { System.out.print ("."); for (int delay = 1; delay <= 500000; delay = delay + 1) {} } // print all information received from thread and stored in Department class deprtmnt.printDeptInfo(); } catch (Exception e) {e.printStackTrace(); } } } // // SQLExecution // - thread (runs independently from main program) // - queries the org table for information about the deptnum chosen // class SQLExecution extends Thread { private Department department; public SQLExecution (Department dept) { department = dept; } public void run() { Connection con = null; try { // connect to the database sample using the default userid, password con = DriverManager.getConnection("jdbc:db2:sample"); con.setAutoCommit(false); String deptName = new String(""); String division = new String(""); String location = new String(""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery ("SELECT deptname, division, location " + "from org WHERE deptnumb = " + department.getDeptNum()); if (rs.next()) { deptName = rs.getString (1); division = rs.getString (2); location = rs.getString (3); // pass in results of query to object Department department.setDeptInfo (deptName, division, location); } else { department.setDeptInfo ("NO SUCH DEPT NUMBER", "", ""); } // disconnect from the server and commit changes rs.close(); con.commit(); con.close(); } catch (Exception e) { e.printStackTrace(); } } } // // Department // - this class allows interaction and passing of information // between the main program and the thread so that information // gathered in the thread can be accessed (printed) from the // main program // class Department { private int deptNum; private String deptName; private String division; private String location; public Department (int num) { deptNum = num; } public int getDeptNum() { return deptNum; } public void setDeptInfo (String name, String dvison, String loc) { deptName = new String (name); division = new String (dvison); location = new String (loc); } public void printDeptInfo() { System.out.print ("\n\nDEPARTMENT # : "); String deptNumString = String.valueOf (deptNum); System.out.println (deptNumString ); System.out.print ("DEPARTMENT NAME : "); System.out.println (deptName ); System.out.print ("DIVISION : "); System.out.println (division ); System.out.print ("LOCATION : "); System.out.println (location ); } }