// Copyright InterBase Software Corporation, 1998. // Written by com.inprise.interbase.interclient.r&d.PaulOstler :-) // // A small application to demonstrate basic, but not necessarily simple, JDBC features. // // Note: you will need to hardwire the path to your copy of employee.gdb // as well as supply a user/password in the code below at the // beginning of method main(). public class DriverExample { // Make a connection to an employee.gdb on your local machine, // and demonstrate basic JDBC features. // Notice that main() uses its own local variables rather than // static class variables, so it need not be synchronized. public static void main (String args[]) { // Modify the following hardwired settings for your environment. // Note: localhost is a TCP/IP keyword which resolves to your local machine's IP address. // If localhost is not recognized, try using your local machine's name or // the loopback IP address 127.0.0.1 in place of localhost. String databaseURL = "jdbc:interbase://localhost/d:/databases/employee.gdb"; String user = "sysdba"; String password = "masterkey"; String driverName = "interbase.interclient.Driver"; // As an exercise to the reader, add some code which extracts databaseURL, // user, and password from the program args[] to main(). // As a further exercise, allow the driver name to be passed as well, // and modify the code below to use driverName rather than the hardwired // string "interbase.interclient.Driver" so that this code becomes // driver independent. However, the code will still rely on the // predefined table structure of employee.gdb. // See comment about closing JDBC objects at the end of this main() method. System.runFinalizersOnExit (true); // Here are the JDBC objects we're going to work with. // We're defining them outside the scope of the try block because // they need to be visible in a finally clause which will be used // to close everything when we are done. // The finally clause will be executed even if an exception occurs. java.sql.Driver d = null; java.sql.Connection c = null; java.sql.Statement s = null; java.sql.ResultSet rs = null; // Any return from this try block will first execute the finally clause // towards the bottom of this file. try { // Let's try to register the InterClient JDBC driver with the driver manager // using one of various registration alternatives... int registrationAlternative = 1; switch (registrationAlternative) { case 1: // This is the standard alternative and simply loads the driver class. // Class.forName() instructs the java class loader to load // and initialize a class. As part of the class initialization // any static clauses associated with the class are executed. // Every driver class is required by the jdbc specification to automatically // create an instance of itself and register that instance with the driver // manager when the driver class is loaded by the java class loader // (this is done via a static clause associated with the driver class). // // Notice that the driver name could have been supplied dynamically, // so that an application is not hardwired to any particular driver // as would be the case if a driver constructor were used, eg. // new interbase.interclient.Driver(). try { Class.forName ("interbase.interclient.Driver"); } catch (java.lang.ClassNotFoundException e) { // A call to Class.forName() forces us to consider this exception :-)... System.out.println ("InterClient not found in class path"); System.out.println (e.getMessage ()); return; } break; case 2: // There is a bug in some JDK 1.1 implementations, eg. with Microsoft // Internet Explorer, such that the implicit driver instance created during // class initialization does not get registered when the driver is loaded // with Class.forName(). // See the FAQ at http://java.sun.com/jdbc for more info on this problem. // Notice that in the following workaround for this bug, that if the bug // is not present, then two instances of the driver will be registered // with the driver manager, the implicit instance created by the driver // class's static clause and the one created explicitly with newInstance(). // This alternative should not be used except to workaround a JDK 1.1 // implementation bug. try { java.sql.DriverManager.registerDriver ( (java.sql.Driver) Class.forName ("interbase.interclient.Driver").newInstance () ); } catch (java.lang.ClassNotFoundException e) { // A call to Class.forName() forces us to consider this exception :-)... System.out.println ("Driver not found in class path"); System.out.println (e.getMessage ()); return; } catch (java.lang.IllegalAccessException e) { // A call to newInstance() forces us to consider this exception :-)... System.out.println ("Unable to access driver constructor, this shouldn't happen!"); System.out.println (e.getMessage ()); return; } catch (java.lang.InstantiationException e) { // A call to newInstance() forces us to consider this exception :-)... // Attempt to instantiate an interface or abstract class. System.out.println ("Unable to create an instance of driver class, this shouldn't happen!"); System.out.println (e.getMessage ()); return; } catch (java.sql.SQLException e) { // A call to registerDriver() forces us to consider this exception :-)... System.out.println ("Driver manager failed to register driver"); showSQLException (e); return; } break; case 3: // Add the InterClient driver name to your system's jdbc.drivers property list. // The driver manager will load drivers from this system property list. // System.getProperties() may not be allowed for applets in some browsers. // For applets, use one of the Class.forName() alternatives above. java.util.Properties sysProps = System.getProperties (); StringBuffer drivers = new StringBuffer ("interbase.interclient.Driver"); String oldDrivers = sysProps.getProperty ("jdbc.drivers"); if (oldDrivers != null) drivers.append (":" + oldDrivers); sysProps.put ("jdbc.drivers", drivers.toString ()); System.setProperties (sysProps); break; case 4: // Advanced: This is a non-standard alternative, and is tied to // a particular driver implementation, but is very flexible. // // It may be possible to configure a driver explicitly, either thru // the use of non-standard driver constructors, or non-standard // driver "set" methods which somehow tailor the driver to behave // differently from the default driver instance. // Under this alternative, a driver instance is created explicitly // using a driver specific constructor. The driver may then be // tailored differently from the default driver instance which is // created automatically when the driver class is loaded by the java class loader. // For example, perhaps a driver instance could be created which // is to behave like some older version of the driver. // // d = new interbase.interclient.Driver (); // d.setVersion (interbase.interclient.Driver.OLD_VERSION); // DriverManager.registerDriver (d); // c = DriverManager.getConnection (...); // // Since two drivers, with differing behavior, are now registered with // the driver manager, they presumably must recognize different jdbc // subprotocols. For example, the tailored driver may only recognize // "jdbc:interbase:old_version://...", whereas the default driver instance // would recognize the standard "jdbc:interbase://...". // There are currently no methods, such as the hypothetical setVersion(), // for tailoring an InterClient driver so this 4th alternative is academic // and not necessary for InterClient. // // It is also possible to create a tailored driver instance which // is *not* registered with the driver manager as follows // // d = new interbase.interclient.Driver (); // d.setVersion (interbase.interclient.Driver.OLD_VERSION); // c = d.connect (...); // // this is the most usual case as this does not require differing // jdbc subprotocols since the connection is obtained thru the driver // directly rather than thru the driver manager. d = new interbase.interclient.Driver (); } // At this point the driver should be registered with the driver manager. // Try to find the registered driver that recognizes interbase URLs... try { // We pass the entire database URL, but we could just pass "jdbc:interbase:" d = java.sql.DriverManager.getDriver (databaseURL); System.out.println ("InterClient version " + d.getMajorVersion () + "." + d.getMinorVersion () + " registered with driver manager."); } catch (java.sq...
steve99