]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/CATS.java
upd: refactor, rename variables, remove challenge-typo
[gigi.git] / src / org / cacert / gigi / dbObjects / CATS.java
index fd715ca513ddc2fa56078adb7210dfa9863277ee..18d5f7928280594d0f7a94f3f9d72fc5231a9ccf 100644 (file)
@@ -4,7 +4,6 @@ import java.sql.Timestamp;
 import java.util.Date;
 import java.util.HashMap;
 
-import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 
@@ -12,39 +11,43 @@ public class CATS {
 
     private static HashMap<String, Integer> names = new HashMap<>();
 
-    public static final String ASSURER_CHALLANGE_NAME = "Assurer's Challange";
+    public static final String ASSURER_CHALLENGE_NAME = "Assurer's Challenge";
 
-    public static final int ASSURER_CHALLANGE_ID;
+    public static final int ASSURER_CHALLENGE_ID;
 
     private CATS() {
 
     }
 
     static {
-        GigiResultSet res = DatabaseConnection.getInstance().prepare("SELECT `id`, `type_text` FROM `cats_type`").executeQuery();
-        while (res.next()) {
-            names.put(res.getString(2), res.getInt(1));
+        try (GigiPreparedStatement st = new GigiPreparedStatement("SELECT `id`, `type_text` FROM `cats_type`")) {
+            GigiResultSet res = st.executeQuery();
+            while (res.next()) {
+                names.put(res.getString(2), res.getInt(1));
+            }
         }
-        ASSURER_CHALLANGE_ID = getID(ASSURER_CHALLANGE_NAME);
+        ASSURER_CHALLENGE_ID = getID(ASSURER_CHALLENGE_NAME);
     }
 
     public static synchronized int getID(String name) {
         Integer i = names.get(name);
         if (i == null) {
-            GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_type` SET `type_text`=?");
-            ps.setString(1, name);
-            ps.execute();
-            i = ps.lastInsertId();
+            try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_type` SET `type_text`=?")) {
+                ps.setString(1, name);
+                ps.execute();
+                i = ps.lastInsertId();
+            }
             names.put(name, i);
         }
         return i;
     }
 
     public static void enterResult(User user, String testType, Date passDate) {
-        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?");
-        ps.setInt(1, user.getId());
-        ps.setInt(2, getID(testType));
-        ps.setTimestamp(3, new Timestamp(passDate.getTime()));
-        ps.execute();
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?")) {
+            ps.setInt(1, user.getId());
+            ps.setInt(2, getID(testType));
+            ps.setTimestamp(3, new Timestamp(passDate.getTime()));
+            ps.execute();
+        }
     }
 }