//To run the program , complile javac programname an // To run java sample //Modified the driver from legacy to Universal //by changing the Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); //to // Class.forName("com.ibm.db2.jcc.DB2Driver"); import java.sql.*; public class EzJava { public static void main(String[] args) { String urlPrefix = "jdbc:db2:"; String url; String s; Connection con; Statement stmt; ResultSet rs; System.out.println ("**** Enter class EzJava"); // Check that first argument is a location name (DB2 UDB for z/OS(TM)) // or database name (DB2 UDB for Linux, UNIX(R) and Windows(R)) if (args.length==0) { System.err.println ("Invalid value. First argument must specify a " + "location or database name."); System.exit(1); } url = urlPrefix + args[0]; try { // Load the DB2 Universal JDBC Driver // Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); Class.forName("com.ibm.db2.jcc.DB2Driver"); System.out.println("**** Loaded the JDBC driver"); // Create the connection using DB2 Universal JDBC Driver type 4connectivity con = DriverManager.getConnection(url); System.out.println("**** Created a JDBC connection to the data source"); // Create the Statement stmt = con.createStatement(); System.out.println("**** Created JDBC Statement object"); // Execute a query and generate a ResultSet instance rs = stmt.executeQuery("SELECT EMPNO FROM DB2LEDU.EMPLOYEE"); System.out.println("**** Created JDBC ResultSet object"); // Print all of the employee numbers to standard output device while (rs.next()) { s = rs.getString(1); System.out.println("Employee number = " + s); } System.out.println("**** Fetched all rows from JDBC ResultSet"); // Close the ResultSet rs.close(); System.out.println("**** Closed JDBC ResultSet"); // Close the Statement stmt.close(); System.out.println("**** Closed JDBC Statement"); // Connection must be on a unit-of-work boundary to allow close con.commit(); System.out.println ( "**** Transaction committed" ); // Close the connection con.close(); System.out.println("**** Disconnected from data source"); System.out.println("**** JDBC Exit from class EzJava - no errors"); } catch (ClassNotFoundException e) { System.err.println("Could not load JDBC driver"); System.out.println("Exception: " + e); e.printStackTrace(); } catch(SQLException ex) { System.err.println("SQLException information"); while(ex!=null) { System.err.println ("Error msg: " + ex.getMessage()); System.err.println ("SQLSTATE: " + ex.getSQLState()); System.err.println ("Error code: " + ex.getErrorCode()); ex.printStackTrace(); ex = ex.getNextException(); // For drivers that support chained exceptions } } } // End main } // End EzJava