package dbpackage.jdbc; import java.sql.*; import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; /** ConnectionFactory is designed to handle the establishment of a Connection. This object will create connections at the request of the client code with the configurations it finds in its properties file. @author Michael D Elder @version 31-Oct-2001 */ public class ConnectionFactory { private static final String defaultPropertiesFile = "jdbc-example.properties"; private Properties propertiesFile = null; /* A java.sql.Connection object serves as the entry point to access the database. Connection objects are heavyweight objects, meaning that their use should be minimized for performance issues. Often times, Connections are treated as a pooled resource. That is, several Connections may be created and group together, then as they are needed they are drawn from the pool, used, and then returned. Thus we minimize the overhead of creating and destroying them. Furthermore, the pool can grow or shrink in size with the current load demand. */ public ConnectionFactory() throws IOException { propertiesFile = new Properties( ); propertiesFile.load(new FileInputStream(defaultPropertiesFile)); } public ConnectionFactory(String _propertiesFile) throws IOException { propertiesFile = new Properties( ); propertiesFile.load(new FileInputStream(_propertiesFile)); } public Connection createConnection() throws Exception { String dbUrl = null; String dbDriver = null; String dbQuery = null; String dbUser = null; String dbPass = null; /* load the URL and the name of the Driver */ dbUrl = propertiesFile.getProperty("database.url"); dbDriver = propertiesFile.getProperty("database.driver"); dbQuery = propertiesFile.getProperty("database.query"); dbUser = propertiesFile.getProperty("database.user"); dbPass = propertiesFile.getProperty("database.pass"); try { printMessage("Loading Driver: \""+dbDriver+"\"."); /* load the Driver, which will cause it to register itself with the the great DriverManager god */ Class.forName(dbDriver).newInstance(); } catch (Exception e) { /* we catch this exception to help debugging property values -- we can indicate to the user what the issue should be, and then rethrow the Exception to the client who called this method. */ printMessage("The driver could not be loaded."); e.printStackTrace(); throw e; } Connection dbConn = null; /* Note that exceptions that result from problems with the DriverManager are not caught, thus they will propagate up the client of this method */ /* no username/password required */ if(dbUser == null) { printMessage("Connecting to URL: \""+dbUrl+"\"."); /* ask the great Driver Manager god for a connection */ dbConn = DriverManager.getConnection(dbUrl); /* they supplied a user name and password, we'll use it*/ } else { printMessage("Connecting to URL: \""+dbUrl+"\" as user \""+ dbUser+"\"."); /* ask the great Driver Manager god for a connection */ dbConn = DriverManager.getConnection(dbUrl, dbUser, dbPass); } return dbConn; } public void printMessage(Object o) { System.out.println(o); } public static void main(String[] args) { ConnectionFactory factory = null; try { // no argument passed if(args.length == 2) { System.out.println("Looking for " + args[0]); factory = new ConnectionFactory(args[0]); } else if(args.length == 1){ factory = new ConnectionFactory(); } else { System.out.println("Usage: java elder.jdbc.ConnectionFactory \"\""); System.out.println("Note that database.properties is optional, \nand that the surrounding quotes are REQUIRED."); return; } } catch(Exception e) { System.out.println("Could not create Factory."); e.printStackTrace(); } String dbQuery = args[1]; try { Connection dbConn = factory.createConnection(); Statement stmt = dbConn.createStatement(); System.out.println("Executing Query: \""+dbQuery+"\"."); ResultSet rs = stmt.executeQuery(dbQuery); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); /* print header */ for(int indx=1; indx<=numberOfColumns; ++indx) { System.out.print(rsmd.getColumnLabel(indx) + "\t\t"); } System.out.println(); System.out.println("---------------------------------------------------------"); /* the spec defines the first call of next() to aim the scroller at the first row */ while(rs.next()) { for(int indx=1; indx<=numberOfColumns; ++indx) { System.out.print(rs.getString(indx) + "\t\t"); } System.out.println(); } System.out.println("Done!"); } catch (SQLException sqle) { System.out.println("Ouch! That hurt!"); System.out.println("SQLException: " + sqle.getMessage()); System.out.println("SQLState: " + sqle.getSQLState()); System.out.println("VendorError: " + sqle.getErrorCode()); } catch (Exception e) { System.out.println("Ouch! That hurt!"); e.printStackTrace(); } } }