Browse Source

Add handle to SQL for server or SQLite for client.

pull/9/head
Gregory Rudolph 4 years ago
parent
commit
2bf41d6d12
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 3
      .gitignore
  2. 37
      src/main/java/MTGClone/SQLDriver.java

3
.gitignore vendored

@ -3,4 +3,5 @@ manifest.txt @@ -3,4 +3,5 @@ manifest.txt
*.jar
target/
.DS_Store
cards.db
cards.db
database.properties

37
src/main/java/MTGClone/SQLDriver.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package MTGClone;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.sql.*;
import java.util.*;
@ -8,13 +9,36 @@ public class SQLDriver { @@ -8,13 +9,36 @@ public class SQLDriver {
protected Connection c = null;
public boolean tryConnect() {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
c.setAutoCommit(false);
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
// Get the current stacktrace
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
// Get the caller of that called Deck, not SQLDriver
StackTraceElement ste = stacktrace[3];
// If it's a controller, we're running in servlet
boolean isServer = ste.getClassName().contains("MTGClone.controller");
if (isServer) {
System.out.println("Server detected!");
try {
Class.forName("com.mysql.jdbc.Driver");
FileInputStream input = new FileInputStream("database.properties");
Properties props = new Properties();
props.load(input );
c = DriverManager.getConnection(props.getProperty("DB_URL"),props.getProperty("DB_USERNAME"),props.getProperty("DB_PASSWORD"));
c.setAutoCommit(false);
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
return false;
}
return false;
} else {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
c.setAutoCommit(false);
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
return false;
}
}
return true;
}
@ -201,6 +225,7 @@ public class SQLDriver { @@ -201,6 +225,7 @@ public class SQLDriver {
+ " CREATURETYPE TEXT NOT NULL)";
stmt.executeUpdate(sql);
stmt.close();
c.commit();
c.close();
if (!setupCards()) {
System.exit(-1);

Loading…
Cancel
Save