一
本文的目的是將一個獲取數據庫連接的普通類重構成DAO+Abstract Factory模式
二
使用數據訪問對象(DAO
由於底層數據源實現變化時
三
首先
DAOClient
import java
publicclass DAOClient {
publicstaticvoid main( String[] args ) {
try {
//For Oracle
Class
Connection conn = DriverManager
System
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
}
}
再將這段代碼封裝到一個getConnection()方法中以便其它的地方調用
import java
publicclass DAOClient {
publicstaticvoid main( String[] args ) {
Connection conn = getConnection();
System
}
/**
*得到一個Connection對象
*@returnjava
*/
privatestatic Connection getConnection() {
Connection conn = null;
try {
//For Oracle
Class
conn = DriverManager
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
return conn;
}
}
再將此方法定義到針對Oracle的工廠類中
OracleDAOFactory
import java
publicclass OracleDAOFactory {
private OracleDAOFactory() {}
/**
*返回一個OracleDAOFactory對象
*@returnOracleDAOFactory類型對象
*/
publicstatic OracleDAOFactory newInstance() {
returnnew OracleDAOFactory();
}
/**
*得到一個Connection對象
*@returnjava
*/
public Connection getConnection() {
Connection conn = null;
try {
//For Oracle
Class
conn = DriverManager
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
return conn;
}
}
此時
import java
public class DAOClient {
publicstaticvoid main( String[] args ) {
Connection conn = OracleDAOFactory
System
}
}
考慮
import java
import java
import java
import java
import java
publicclass OracleDAOFactory {
privatestatic Properties prop = new Properties();
static {
try {
prop
} catch ( IOException e ) {
System
e
}
}
private String CONNECTION_SERVER_NAME = prop
private String CONNECTION_DRIVER = prop
private String CONNECTION_DBINSTANCE = prop
private String CONNECTION_USER = prop
private String CONNECTION_PWD = prop
private String CONNECTION_URL =
+ CONNECTION_SERVER_NAME +
private OracleDAOFactory() {}
/**
*返回一個OracleDAOFactory對象
*@returnOracleDAOFactory類型對象
*/
publicstatic OracleDAOFactory newInstance() {
returnnew OracleDAOFactory();
}
/**
*得到一個Connection對象
*@returnjava
*/
public Connection getConnection() {
Connection conn = null;
try {
Class
conn = DriverManager
CONNECTION_URL
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
return conn;
}
}
添加配置文件config
oracle_server_name=localhost
oracle_conn_driver=oracle
oracle_dbInstance=dy
oracle_conn_user=scott
oracle_conn_pwd=tiger
繼續考慮
DAOFactory
import java
publicabstractclass DAOFactory {
publicstaticfinalintORACLE =
publicstaticfinalintSQLSERVER =
publicstaticfinalintMYSQL =
public abstract Connection getConnection();
publicstatic DAOFactory getDAOFactory( int dbType ) {
switch( dbType ) {
caseORACLE:
return OracleDAOFactory
caseSQLSERVER:
return SqlDAOFactory
caseMYSQL:
return MySqlDAOFactory
default:
returnnull;
}
}
}
SqlDAOFactory
import java
import java
import java
import java
import java
publicclass SqlDAOFactory extends DAOFactory {
privatestatic Properties prop = new Properties();
static {
try {
prop
} catch ( IOException e ) {
System
e
}
}
private String CONNECTION_SERVER_NAME = prop
private String CONNECTION_DRIVER = prop
private String CONNECTION_DBINSTANCE = prop
private String CONNECTION_USER = prop
private String CONNECTION_PWD = prop
private String CONNECTION_URL =
+ CONNECTION_SERVER_NAME +
+ CONNECTION_DBINSTANCE;
private SqlDAOFactory() {}
/**
*返回一個SqlDAOFactory對象
*@returnSqlDAOFactory類型對象
*/
publicstatic SqlDAOFactory newInstance() {
returnnew SqlDAOFactory();
}
/**
*得到一個Connection對象
*@returnjava
*/
public Connection getConnection() {
Connection conn = null;
try {
Class
conn = DriverManager
CONNECTION_URL
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
return conn;
}
}
MySqlDAPFactory
import java
import java
import java
import java
import java
publicclass MySqlDAOFactory extends DAOFactory {
privatestatic Properties prop = new Properties();
static {
try {
prop
} catch ( IOException e ) {
System
e
}
}
private String CONNECTION_SERVER_NAME = prop
private String CONNECTION_DRIVER = prop
private String CONNECTION_DBINSTANCE = prop
private String CONNECTION_USER = prop
private String CONNECTION_PWD = prop
private String CONNECTION_URL =
+ CONNECTION_SERVER_NAME +
+
private MySqlDAOFactory() {}
/**
*返回一個MySqlDAOFactory對象
*@returnMySqlDAOFactory類型對象
*/
publicstatic MySqlDAOFactory newInstance() {
returnnew MySqlDAOFactory();
}
/**
*得到一個Connection對象
*@returnjava
*/
public Connection getConnection() {
Connection conn = null;
try {
Class
conn = DriverManager
CONNECTION_URL
} catch ( ClassNotFoundException e ) {
e
} catch ( SQLException e ) {
e
}
return conn;
}
}
修改config
#Oracle
oracle_server_name=localhost
oracle_conn_driver=oracle
oracle_dbInstance=dy
oracle_conn_user=scott
oracle_conn_pwd=tiger
#SqlServer
sqlserver_server_name=localhost
sqlserver_conn_driver=com
sqlserver_dbInstance=test
sqlserver_conn_user=sa
sqlserver_conn_pwd=sa
#MySql
mysql_server_name=localhost
mysql_conn_driver=com
mysql_dbInstance=test
mysql_conn_user=root
mysql_conn_pwd=root
最後
import java
public class DAOClient {
public static void main( String[] args ) {
DAOFactory dao = DAOFactory
Connection conn = dao
System
}
}
通過這種DAO+(Abstract)Factory方式
DAOFactory dao = DAOFactory
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26544.html