package dbpackage.jdbc; import java.sql.*; import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; /** The purpose of this object is to test the properites required to connect to an arbitrary database. The properties that this object will seek are : database.url: The URL to use in finding the database database.driver: The name of the Driver class to use to connect database.query: An example query to ensure that a connection has been established And the follow two are optional if the database type requires them: database.user: The username of the user database.pass: The corresponding password for this user An example for Oracle might be: database.url=jdbc:oracle:thin:@servercsdept:1521:orcl database.driver=oracle.jdbc.driver.OracleDriver database.query=select * from Student database.user=username database.pass=password @author Michael D Elder @version 31-Oct-2001 */ public class JDBCTest { public static void main(String[] args) { String propFile = null; String dbUrl = null; String dbDriver = null; String dbQuery = null; String dbUser = null; String dbPass = null; if(args.length == 0) { propFile = "jdbc-example.properties"; System.out.println("No properties file was supplied, using default of \"jdbc-example.properties\"."); } else { propFile = args[0]; } Properties props = new Properties(); try { props.load(new FileInputStream(propFile)); } catch(IOException ioe) { System.out.println("Ouch! That hurt!"); System.out.println("Did you give me a valid file, yo?"); ioe.printStackTrace(); return; } /* load the URL and the name of the Driver */ dbUrl = props.getProperty("database.url"); dbDriver = props.getProperty("database.driver"); dbQuery = props.getProperty("database.query"); dbUser = props.getProperty("database.user"); dbPass = props.getProperty("database.pass"); /* java.sql.Connection serves as the entry point to Application-scope JDBC */ Connection dbConn = null; try { System.out.println("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) { System.out.println("The driver could not be loaded."); e.printStackTrace(); return; } try { /* no username/password required */ if(dbUser == null) { System.out.println("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 { System.out.println("Connecting to URL: \""+dbUrl+"\" as user \""+ dbUser+"\"."); /* ask the great Driver Manager god for a connection */ dbConn = DriverManager.getConnection(dbUrl, dbUser, dbPass); } 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"); } 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(); } } }