import java.sql.*; import java.util.Properties; import java.io.InputStream; public class ErrorChecker { public static void main (String args[]) { String driverPrefixURL = "jdbc:odbc:"; String username = null; String password = null; String dataSource = null; try { // Invalid driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriverxx"); } catch (/* Which Exception? */) { System.out.println("If an invalid driver name is provided or the " + "class cannot be found, the ClassNotFoundException is thrown."); } try { // Valid driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (/* Which Exception? */) { System.out.println("Failed to load JDBC/ODBC driver."); } try { // Resource loading // Look for resource file 'odbc.datasource' InputStream is = ClassLoader.getSystemResourceAsStream ("odbc.datasource"); Properties p = new Properties(); p.load (is); dataSource = p.getProperty("datasource.name"); if (dataSource == null) throw new Exception (); username = p.getProperty("datasource.username", ""); password = p.getProperty("datasource.password", ""); } catch (Exception e) { System.out.println("Unable to read resource to get data source"); return; } try { // Invalid connection Connection badcon = DriverManager.getConnection("foobar", username, password); // If your datasource supports usernames and passwords, you can try this one too. // Connection badcon = DriverManager.getConnection(driverPrefixURL+dataSource, "DD", password); } catch (/* Which Exception? */) { /* Display Error Code, SQL State, and Error Message. } try { // Valid connection Connection con = null; try { con = DriverManager.getConnection(driverPrefixURL+dataSource+";foo=bar", username, password); } catch (SQLException e) { // For some drivers, this fails vs. generate a warning. System.out.println ("foo=bar option failed"); con = DriverManager.getConnection(driverPrefixURL+dataSource, username, password); } System.out.println("Connected."); DatabaseMetaData dmd = con.getMetaData(); if (dmd == null) { System.out.println ("No Database Meta Data"); return; } else { // Determine what level of ANSI92 is supported // Determine what SQL Grammer level is supported } Statement stat = con.createStatement(); // Drop table students2 - so we can run this multiple times // Create table students2 if ((dmd != null) && dmd.supportsCoreSQLGrammar()) { System.out.println ("Inserting the easy way"); stat.executeUpdate ("insert into students2 select name, class from students"); } else { // Minimum System.out.println ("Inserting the hard way"); // Insert each student into students2. } // Close everything } catch (/* Which Exception? */) { /* Display Error Code, SQL State, and Error Message. e.printStackTrace(); } } private static void printSQLWarnings (SQLWarning w) { // Print out all warning messages in chain. // Check for DataTruncation warnings, too. } }