]> WPIA git - gigi.git/commitdiff
add simple Database connection
authorFelix Dörre <felix@dogcraft.de>
Sat, 21 Jun 2014 17:40:17 +0000 (19:40 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 21 Jun 2014 17:56:48 +0000 (19:56 +0200)
config/.gitignore
config/sql.properties.template [new file with mode: 0644]
src/org/cacert/gigi/database/DatabaseConnection.java [new file with mode: 0644]

index cc6b0ea7c3abf767996a636d873de029a4f6e4d6..ef8da91c0f7ba976e2a97412261e5a466e626e45 100644 (file)
@@ -1,3 +1,4 @@
 
 keystore.pkcs12
 cacerts.jks
+sql.properties
diff --git a/config/sql.properties.template b/config/sql.properties.template
new file mode 100644 (file)
index 0000000..8257406
--- /dev/null
@@ -0,0 +1,4 @@
+driver=com.mysql.jdbc.Driver
+url=
+user=
+password=
diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java
new file mode 100644 (file)
index 0000000..6d505d8
--- /dev/null
@@ -0,0 +1,55 @@
+package org.cacert.gigi.database;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Properties;
+
+public class DatabaseConnection {
+       Connection c;
+       HashMap<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();
+       static Properties credentials = new Properties();
+       static {
+               try {
+                       credentials.load(new FileInputStream("config/sql.properties"));
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+       public DatabaseConnection() {
+               try {
+                       Class.forName(credentials.getProperty("driver"));
+               } catch (ClassNotFoundException e) {
+                       e.printStackTrace();
+               }
+               try {
+                       c = DriverManager.getConnection(credentials.getProperty("url"),
+                                       credentials.getProperty("user"),
+                                       credentials.getProperty("password"));
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+
+       }
+       public PreparedStatement prepare(String query) throws SQLException {
+               PreparedStatement statement = statements.get(query);
+               if (statement == null) {
+                       statement = c.prepareStatement(query);
+                       statements.put(query, statement);
+               }
+               return statement;
+       }
+       static ThreadLocal<DatabaseConnection> instances = new ThreadLocal<DatabaseConnection>() {
+               @Override
+               protected DatabaseConnection initialValue() {
+                       return new DatabaseConnection();
+               }
+       };
+       public static DatabaseConnection getInstance() {
+               return instances.get();
+       }
+}