定义一个功能性接口
package model;
import java.sql.Connection;
import java.sql.SQLException;
public interface BuildConnection {
Connection getConnection() throws SQLException;
}
实现一个具体的connection产品,它实现BuildConnection的接口
package model;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DataSourceConnection implements BuildConnection {
private static Properties prop = new Properties();
private DataSource ds;
private String initialContextFactory;
private String providerUrl;
private String securityPrincipal;
private String securityCredentials;
private String dataSource;
public DataSourceConnection(String securityPrincipal, String securityCredentials, String dataSource, String providerUrl, String initialContextFactory) throws NamingException {
this.securityPrincipal = securityPrincipal;
this.securityCredentials = securityCredentials;
this.dataSource = dataSource;
this.providerUrl = providerUrl;
this.initialContextFactory = initialContextFactory;
loadDriver();
}
public Connection getConnection() throws SQLException {
return ds.getConnection();
}
private void loadDriver() throws NamingException {
if (!(prop.contains(initialContextFactory) && prop.contains(providerUrl) && prop.contains(securityPrincipal) && prop.contains(securityCredentials))) {
prop.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
prop.put(Context.PROVIDER_URL, providerUrl);
prop.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
prop.put(Context.SECURITY_CREDENTIALS, securityCredentials);
}
Context context = new InitialContext(prop);
ds =(DataSource)context.lookup(dataSource);
}
}
实现另一个具体的connection产品,它实现BuildConnection的接口
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
public class DriverManagerConnection implements BuildConnection {
private static Set<String> set = new HashSet<String>();
private String driver;
private String url;
private String userId;
private String password;
public DriverManagerConnection(String driver, String url, String userId, String password) throws ClassNotFoundException {
this.driver = driver;
this.url = url;
this.userId = userId;
this.password = password;
loadDriver();
}
private void loadDriver() throws ClassNotFoundException {
if (!set.contains(driver)) {
Class.forName(driver);
set.add(driver);
}
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, userId, password);
}
}
定义一个可以生产connection的工厂
package model;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
public class ConnectionFactory {
private BuildConnection delegate;
public ConnectionFactory(String aDriver, String aUrl, String aUserid, String aPassword) throws ClassNotFoundException {
super();
delegate = new DriverManagerConnection(aDriver, aUrl, aUserid, aPassword);
}
public ConnectionFactory(String userId, String password, String dataSource, String providerUrl, String initialContextFactory) throws NamingException {
super();
delegate = new DataSourceConnection(userId, password, dataSource, providerUrl, initialContextFactory);
}
public Connection getConnection() throws SQLException {
Connection connection = delegate.getConnection();
connection.setAutoCommit(false);
return connection;
}
}
定义一个管理connection的类
package model;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.NamingException;
import tool.DriverNotFound;
public class DatabaseManagement {
private static final String TYPE_JDBC = "jdbc";
private static final String TYPE_DS = "ds";
private static final String TYPE = "type";
private static final String USERID = "userid";
private static final String PASSWORD = "password";
private static final String DRIVER = "driver";
private static final String URL = "url";
private static final String DATASOURCE = "datasource";
private static final String PROVIDER_URL = "provider_url";
private static final String INITIAL_CONTEXT_FACTORY = "initial_context_factory";
private static final String file = "databaseInformation.properties";
private static String type = null;
private static String userid = null;
private static String password = null;
private static String driver = null;
private static String url = null;
private static String datasource = null;
private static String initialContextFactory = null;
private static String providerUrl = null;
static {
refresh();
}
public static void refresh() {
try {
InputStream inputStream = DatabaseManagement.class.getClassLoader().getResourceAsStream(file);
if (inputStream != null) {
Properties properties = new Properties();
properties.load(inputStream);
type = properties.getProperty(TYPE, type);
if (type.equalsIgnoreCase(TYPE_JDBC)) {
userid = properties.getProperty(USERID, userid);
password = properties.getProperty(PASSWORD, password);
driver = properties.getProperty(DRIVER, driver);
url = properties.getProperty(URL, url);
}
if (type.equalsIgnoreCase(TYPE_DS)) {
userid = properties.getProperty(USERID, userid);
password = properties.getProperty(PASSWORD, password);
datasource = properties.getProperty(DATASOURCE, datasource);
providerUrl = properties.getProperty(PROVIDER_URL, providerUrl);
initialContextFactory = properties.getProperty(INITIAL_CONTEXT_FACTORY, initialContextFactory);
}
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException, DriverNotFound {
ConnectionFactory factory = null;
try {
if (type.equalsIgnoreCase(TYPE_JDBC)) {
factory = new ConnectionFactory(driver, url, userid, password);
} else {
factory = new ConnectionFactory(userid, password, datasource, providerUrl, initialContextFactory);
}
} catch (ClassNotFoundException e) {
throw new DriverNotFound(driver);
} catch (NamingException e) {
e.printStackTrace();
}
return factory.getConnection();
}
}
定义一个异常类
package tool;
public class DriverNotFound extends Exception {
private static final long serialVersionUID = -6926830075212095043L;
private String driver;
public DriverNotFound(String driver) {
super("Driver " + driver + " not found");
setDriver(driver);
}
public String getDriver() {
return driver;
}
private void setDriver(String driver) {
this.driver = driver;
}
}