Files
sweetgum-server/src/tools/DatabaseConnection.java
ronancpl 972517e7b2 source
Source for my MapleSolaxiaV2 (v83 MapleStory).
2015-11-02 23:17:21 -02:00

51 lines
1.5 KiB
Java

package tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import constants.ServerConstants;
/**
* @author Frz (Big Daddy)
* @author The Real Spookster (some modifications to this beautiful code)
*/
public class DatabaseConnection {
public static final int RETURN_GENERATED_KEYS = 1;
private static ThreadLocal<Connection> con = new ThreadLocalConnection();
public static Connection getConnection() {
Connection c = con.get();
try {
c.getMetaData();
} catch (SQLException e) { // connection is dead, therefore discard old object 5ever
con.remove();
c = con.get();
}
return c;
}
private static class ThreadLocalConnection extends ThreadLocal<Connection> {
@Override
protected Connection initialValue() {
try {
Class.forName("com.mysql.jdbc.Driver"); // touch the mysql driver
} catch (ClassNotFoundException e) {
System.out.println("[SEVERE] SQL Driver Not Found. Consider death by clams.");
e.printStackTrace();
return null;
}
try {
return DriverManager.getConnection(ServerConstants.DB_URL, ServerConstants.DB_USER, ServerConstants.DB_PASS);
} catch (SQLException e) {
System.out.println("[SEVERE] Unable to make database connection.");
e.printStackTrace();
return null;
}
}
}
}