1 21 22 import java.sql.Connection ; 23 import java.sql.DriverManager ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 28 import java.util.Properties ; 29 30 31 53 public class SimpleApp 54 { 55 56 public String framework = "embedded"; 57 public String driver = "org.apache.derby.jdbc.EmbeddedDriver"; 58 public String protocol = "jdbc:derby:"; 59 60 public static void main(String [] args) 61 { 62 new SimpleApp().go(args); 63 } 64 65 void go(String [] args) 66 { 67 68 parseArguments(args); 69 70 System.out.println("SimpleApp starting in " + framework + " mode."); 71 72 try 73 { 74 78 Class.forName(driver).newInstance(); 79 System.out.println("Loaded the appropriate driver."); 80 81 Connection conn = null; 82 Properties props = new Properties (); 83 props.put("user", "user1"); 84 props.put("password", "user1"); 85 86 95 conn = DriverManager.getConnection(protocol + 96 "derbyDB;create=true", props); 97 98 System.out.println("Connected to and created database derbyDB"); 99 100 conn.setAutoCommit(false); 101 102 106 Statement s = conn.createStatement(); 107 108 111 s.execute("create table derbyDB(num int, addr varchar(40))"); 112 System.out.println("Created table derbyDB"); 113 s.execute("insert into derbyDB values (1956,'Webster St.')"); 114 System.out.println("Inserted 1956 Webster"); 115 s.execute("insert into derbyDB values (1910,'Union St.')"); 116 System.out.println("Inserted 1910 Union"); 117 s.execute( 118 "update derbyDB set num=180, addr='Grand Ave.' where num=1956"); 119 System.out.println("Updated 1956 Webster to 180 Grand"); 120 121 s.execute( 122 "update derbyDB set num=300, addr='Lakeshore Ave.' where num=180"); 123 System.out.println("Updated 180 Grand to 300 Lakeshore"); 124 125 128 ResultSet rs = s.executeQuery( 129 "SELECT num, addr FROM derbyDB ORDER BY num"); 130 131 if (!rs.next()) 132 { 133 throw new Exception ("Wrong number of rows"); 134 } 135 136 if (rs.getInt(1) != 300) 137 { 138 throw new Exception ("Wrong row returned"); 139 } 140 141 if (!rs.next()) 142 { 143 throw new Exception ("Wrong number of rows"); 144 } 145 146 if (rs.getInt(1) != 1910) 147 { 148 throw new Exception ("Wrong row returned"); 149 } 150 151 if (rs.next()) 152 { 153 throw new Exception ("Wrong number of rows"); 154 } 155 156 System.out.println("Verified the rows"); 157 158 s.execute("drop table derbyDB"); 159 System.out.println("Dropped table derbyDB"); 160 161 164 rs.close(); 165 s.close(); 166 System.out.println("Closed result set and statement"); 167 168 171 conn.commit(); 172 conn.close(); 173 System.out.println("Committed transaction and closed connection"); 174 175 183 boolean gotSQLExc = false; 184 185 if (framework.equals("embedded")) 186 { 187 try 188 { 189 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 190 } 191 catch (SQLException se) 192 { 193 gotSQLExc = true; 194 } 195 196 if (!gotSQLExc) 197 { 198 System.out.println("Database did not shut down normally"); 199 } 200 else 201 { 202 System.out.println("Database shut down normally"); 203 } 204 } 205 } 206 catch (Throwable e) 207 { 208 System.out.println("exception thrown:"); 209 210 if (e instanceof SQLException ) 211 { 212 printSQLError((SQLException ) e); 213 } 214 else 215 { 216 e.printStackTrace(); 217 } 218 } 219 220 System.out.println("SimpleApp finished"); 221 } 222 223 static void printSQLError(SQLException e) 224 { 225 while (e != null) 226 { 227 System.out.println(e.toString()); 228 e = e.getNextException(); 229 } 230 } 231 232 private void parseArguments(String [] args) 233 { 234 int length = args.length; 235 236 for (int index = 0; index < length; index++) 237 { 238 if (args[index].equalsIgnoreCase("jccjdbcclient")) 239 { 240 framework = "jccjdbc"; 241 driver = "com.ibm.db2.jcc.DB2Driver"; 242 protocol = "jdbc:derby:net://localhost:1527/"; 243 } 244 if (args[index].equalsIgnoreCase("derbyclient")) 245 { 246 framework = "derbyclient"; 247 driver = "org.apache.derby.jdbc.ClientDriver"; 248 protocol = "jdbc:derby://localhost:1527/"; 249 } 250 } 251 } 252 } 253 | Popular Tags |