Min singleton som jeg bruger til connection til MySql
package maxicom.wincar.dm.dbaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Insert the type's description here.
* Creation date: (31-01-00 21:46:44)
* @author:
*/
public class DBAccess {
private static DBAccess singleInstance = null;
private String driverName = "";
private String dbUrl = ""; //hjemme
private static Connection conn = null;
/**
* DBAccess constructor comment.
*/
private DBAccess() {
super();
try {
driverName = "org.gjt.mm.mysql.Driver";
dbUrl = "jdbc:
mysql://server2003:3306/[datanbase]?user=[bruger]&password=[adgangskode]"; // Get an instance of the JDBC driver
Class.forName(driverName).newInstance();
// Create a connection object
conn = DriverManager.getConnection(dbUrl);
} catch (Exception e) {
//Hvis den går ned lukker vi denne connection og åbner en ny
close();
singleInstance = new DBAccess();
e.printStackTrace();
}
}
public java.sql.Connection getConn() {
return conn;
}
public static DBAccess getInstance() {
if (singleInstance == null) {
singleInstance = new DBAccess();
}
return singleInstance;
}
public java.sql.Statement getStmt() throws SQLException {
// Create a statement object
return (conn.createStatement());
}
public static void close(){
try {
conn.close();
singleInstance = new DBAccess();
} catch (Exception e) {
e.printStackTrace();
}
}
}