]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/CATS.java
fix: SQL change database call pattern
[gigi.git] / src / org / cacert / gigi / dbObjects / CATS.java
index fd715ca513ddc2fa56078adb7210dfa9863277ee..0bda2e6770205e0cd5bde09f7974356c913fff25 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;
 
@@ -21,9 +20,11 @@ public class 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);
     }
@@ -31,20 +32,22 @@ public class CATS {
     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();
+        }
     }
 }