E568 Used SQL that's more universal for several DBs in DBSetup · javaee-samples/javaee7-samples@24dd78d · GitHub
[go: up one dir, main page]

Skip to content

Commit 24dd78d

Browse files
committed
Used SQL that's more universal for several DBs in DBSetup
1 parent a70e3ba commit 24dd78d

File tree

1 file changed

+20
-10
lines changed
  • servlet/security-digest/src/main/java/org/javaee7/servlet/security/digest

1 file changed

+20
-10
lines changed

servlet/security-digest/src/main/java/org/javaee7/servlet/security/digest/DatabaseSetup.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,23 @@ public void contextInitialized(ServletContextEvent sce) {
4040

4141
System.out.println("ha1=" + ha1);
4242

43-
execute(dataSource, "DROP TABLE IF EXISTS usertable");
44-
execute(dataSource, "DROP TABLE IF EXISTS grouptable");
43+
tryDropTables();
4544

4645
System.out.println("Adding user u1 with group g1 to database");
4746

48-
execute(dataSource, "CREATE TABLE IF NOT EXISTS usertable(username VARCHAR(32) PRIMARY KEY, password VARCHAR(127))");
49-
execute(dataSource, "CREATE TABLE IF NOT EXISTS grouptable(username VARCHAR(64), groupname VARCHAR(64))");
47+
execute(dataSource, "CREATE TABLE usertable(username VARCHAR(32) PRIMARY KEY, password VARCHAR(127))");
48+
execute(dataSource, "CREATE TABLE grouptable(username VARCHAR(64), groupname VARCHAR(64))");
5049

5150
execute(dataSource, "INSERT INTO usertable VALUES('u1', '" + ha1 + "')");
5251

5352
execute(dataSource, "INSERT INTO grouptable VALUES('u1', 'g1')");
53+
54+
System.out.println("Done creating DB tables");
5455
}
5556

5657
@Override
5758
public void contextDestroyed(ServletContextEvent sce) {
58-
try {
59-
execute(dataSource, "DROP TABLE IF EXISTS usertable");
60-
execute(dataSource, "DROP TABLE IF EXISTS grouptable");
61-
} catch (Exception e) {
62-
}
63-
59+
tryDropTables();
6460
}
6561

6662
private void execute(DataSource dataSource, String query) {
@@ -74,4 +70,18 @@ private void execute(DataSource dataSource, String query) {
7470
}
7571
}
7672

73+
private void tryDropTables() {
74+
try {
75+
execute(dataSource, "DROP TABLE IF EXISTS usertable");
76+
execute(dataSource, "DROP TABLE IF EXISTS grouptable");
77+
} catch (Exception e) {
78+
try {
79+
execute(dataSource, "DROP TABLE usertable");
80+
execute(dataSource, "DROP TABLE grouptable");
81+
} catch (Exception ee) {
82+
}
83+
}
84+
85+
}
86+
7787
}

0 commit comments

Comments
 (0)
0