1 21 import java.sql.*; 24 25 public class WwdClientExample 26 { 27 public static void main(String [] args) 28 { 29 String driver = "org.apache.derby.jdbc.ClientDriver"; 32 String dbName="jdbcDemoDB"; 34 String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true"; 36 37 Connection conn = null; 38 Statement s; 39 PreparedStatement psInsert; 40 ResultSet myWishes; 41 String printLine = " __________________________________________________"; 42 String createString = "CREATE TABLE WISH_LIST " 43 + "(WISH_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " 44 + " CONSTRAINT WISH_PK PRIMARY KEY, " 45 + " ENTRY_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " 46 + " WISH_ITEM VARCHAR(32) NOT NULL) " ; 47 String answer; 48 49 try { 52 57 Class.forName(driver); 58 System.out.println(driver + " loaded. "); 59 } catch(java.lang.ClassNotFoundException e) { 60 System.err.print("ClassNotFoundException: "); 61 System.err.println(e.getMessage()); 62 System.out.println("\n >>> Please check your CLASSPATH variable <<<\n"); 63 } 64 try { 67 conn = DriverManager.getConnection(connectionURL); 69 System.out.println("Connected to database " + dbName); 70 71 s = conn.createStatement(); 74 if (! WwdUtils.wwdChk4Table(conn)) 77 { 78 System.out.println (" . . . . creating table WISH_LIST"); 79 s.execute(createString); 80 } 81 psInsert = conn.prepareStatement("insert into WISH_LIST(WISH_ITEM) values (?)"); 83 84 do { 87 answer = WwdUtils.getWishItem(); 89 if (! answer.equals("exit")) 91 { 92 psInsert.setString(1,answer); 94 psInsert.executeUpdate(); 95 96 myWishes = s.executeQuery("select ENTRY_DATE, WISH_ITEM from WISH_LIST order by ENTRY_DATE"); 98 99 System.out.println(printLine); 101 while (myWishes.next()) 102 { 103 System.out.println("On " + myWishes.getTimestamp(1) + " I wished for " + myWishes.getString(2)); 104 } 105 System.out.println(printLine); 106 myWishes.close(); 108 } } while (! answer.equals("exit")) ; 112 psInsert.close(); 114 s.close(); 115 conn.close(); 116 System.out.println("Closed connection"); 117 118 121 if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) { 122 boolean gotSQLExc = false; 123 try { 124 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 125 } catch (SQLException se) { 126 if ( se.getSQLState().equals("XJ015") ) { 127 gotSQLExc = true; 128 } 129 } 130 if (!gotSQLExc) { 131 System.out.println("Database did not shut down normally"); 132 } else { 133 System.out.println("Database shut down normally"); 134 } 135 } 136 137 } catch (Throwable e) { 139 141 System.out.println(" . . . exception thrown:"); 142 errorPrint(e); 143 } 144 System.out.println("Working With Derby JDBC program ending."); 145 } 146 150 static void errorPrint(Throwable e) { 151 if (e instanceof SQLException) 152 SQLExceptionPrint((SQLException)e); 153 else { 154 System.out.println("A non SQL error occured."); 155 e.printStackTrace(); 156 } 157 } 159 static void SQLExceptionPrint(SQLException sqle) { 161 while (sqle != null) { 162 System.out.println("\n---SQLException Caught---\n"); 163 System.out.println("SQLState: " + (sqle).getSQLState()); 164 System.out.println("Severity: " + (sqle).getErrorCode()); 165 System.out.println("Message: " + (sqle).getMessage()); 166 sqle.printStackTrace(); 167 sqle = sqle.getNextException(); 168 } 169 } } 171 | Popular Tags |