1 22 25 import java.sql.*; 28 29 public class WwdEmbedded 30 { 31 public static void main(String [] args) 32 { 33 String driver = "org.apache.derby.jdbc.EmbeddedDriver"; 36 String dbName="jdbcDemoDB"; 38 String connectionURL = "jdbc:derby:" + dbName + ";create=true"; 40 41 Connection conn = null; 42 Statement s; 43 PreparedStatement psInsert; 44 ResultSet myWishes; 45 String printLine = " __________________________________________________"; 46 String createString = "CREATE TABLE WISH_LIST " 47 + "(WISH_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " 48 + " CONSTRAINT WISH_PK PRIMARY KEY, " 49 + " ENTRY_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " 50 + " WISH_ITEM VARCHAR(32) NOT NULL) " ; 51 String answer; 52 53 try { 56 61 Class.forName(driver); 62 System.out.println(driver + " loaded. "); 63 } catch(java.lang.ClassNotFoundException e) { 64 System.err.print("ClassNotFoundException: "); 65 System.err.println(e.getMessage()); 66 System.out.println("\n >>> Please check your CLASSPATH variable <<<\n"); 67 } 68 try { 71 conn = DriverManager.getConnection(connectionURL); 73 System.out.println("Connected to database " + dbName); 74 75 s = conn.createStatement(); 78 if (! WwdUtils.wwdChk4Table(conn)) 81 { 82 System.out.println (" . . . . creating table WISH_LIST"); 83 s.execute(createString); 84 } 85 psInsert = conn.prepareStatement("insert into WISH_LIST(WISH_ITEM) values (?)"); 87 88 do { 91 answer = WwdUtils.getWishItem(); 93 if (! answer.equals("exit")) 95 { 96 psInsert.setString(1,answer); 98 psInsert.executeUpdate(); 99 100 myWishes = s.executeQuery("select ENTRY_DATE, WISH_ITEM from WISH_LIST order by ENTRY_DATE"); 102 103 System.out.println(printLine); 105 while (myWishes.next()) 106 { 107 System.out.println("On " + myWishes.getTimestamp(1) + " I wished for " + myWishes.getString(2)); 108 } 109 System.out.println(printLine); 110 myWishes.close(); 112 } } while (! answer.equals("exit")) ; 116 psInsert.close(); 118 s.close(); 119 conn.close(); 120 System.out.println("Closed connection"); 121 122 125 if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) { 126 boolean gotSQLExc = false; 127 try { 128 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 129 } catch (SQLException se) { 130 if ( se.getSQLState().equals("XJ015") ) { 131 gotSQLExc = true; 132 } 133 } 134 if (!gotSQLExc) { 135 System.out.println("Database did not shut down normally"); 136 } else { 137 System.out.println("Database shut down normally"); 138 } 139 } 140 141 } catch (Throwable e) { 143 145 System.out.println(" . . . exception thrown:"); 146 errorPrint(e); 147 } 148 System.out.println("Working With Derby JDBC program ending."); 149 } 150 154 static void errorPrint(Throwable e) { 155 if (e instanceof SQLException) 156 SQLExceptionPrint((SQLException)e); 157 else { 158 System.out.println("A non SQL error occured."); 159 e.printStackTrace(); 160 } 161 } 163 static void SQLExceptionPrint(SQLException sqle) { 165 while (sqle != null) { 166 System.out.println("\n---SQLException Caught---\n"); 167 System.out.println("SQLState: " + (sqle).getSQLState()); 168 System.out.println("Severity: " + (sqle).getErrorCode()); 169 System.out.println("Message: " + (sqle).getMessage()); 170 sqle.printStackTrace(); 171 sqle = sqle.getNextException(); 172 } 173 } } 175 | Popular Tags |