分类:
2008-09-12 13:09:46
一、前言
本文的目的是将一个获取数据库连接的普通类重构成DAO+Abstract Factory模式。
二、设计初衷
使用数据访问对象(DAO,Data Access Object)模式来抽象和封装所有对数据源的访问。DAO管理着与数据源的连接以便检索和数据。可以降低商业逻辑层和数据访问层的耦合度,提高应用的可维护性和可移植性。
由于底层数据源实现变化时,DAO向客户端提供的接口不会变化,所有该模式允许DAO调整到不同的模式,而不会影响其客户端或者业务组件。显然,DAO充当了组件和数据源之间的适配器。
三、重构
首先,创建一个获取数据库连接的普通类:
DAOClient.java
import java.sql.*;
publicclass DAOClient {
publicstaticvoid main( String[] args ) {
try {
//For
Class.forName( "oracle.jdbc.driver.Driver" );
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:dy", "scott", "tiger" );
System.out.println( conn.toString() );
} catch ( ClassNotFoundException e ) {
e.printStackTrace();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
再将这段代码封装到一个getConnection()方法中以便其它的地方调用:
import java.sql.*;
publicclass DAOClient {
publicstaticvoid main( String[] args ) {
Connection conn = getConnection();
System.out.println( conn.toString() );
}
/**
*得到一个Connection对象
*/
privatestatic Connection getConnection() {
Connection conn = null;
try {
//For Oracle
Class.forName( "oracle.jdbc.driver.OracleDriver" );
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:dy", "scott", "tiger" );
} catch ( ClassNotFoundException e ) {
e.printStackTrace();
} catch ( SQLException e ) {
e.printStackTrace();
}
return conn;
}
}
再将此方法定义到针对Oracle的工厂类中:
OracleDAOFactory.java
import java.sql.*;
publicclass OracleDAOFactory {
private OracleDAOFactory() {}
/**
*返回一个OracleDAOFactory对象
类型对象
*/
publicstatic OracleDAOFactory newInstance() {
returnnew OracleDAOFactory();
}
/**
*得到一个Connection对象
*/
public Connection getConnection() {
Connection conn = null;
try {
//For Oracle
Class.forName( "oracle.jdbc.driver.OracleDriver" );
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:dy", "scott", "tiger" );
} catch ( ClassNotFoundException e ) {
e.printStackTrace();
} catch ( SQLException e ) {
e.printStackTrace();
}
return conn;
}
}
此时,DAOClient.java这个测试类的代码应修改为:
import java.sql.*;
public class DAOClient {
publicstaticvoid main( String[] args ) {
Connection conn = OracleDAOFactory.newInstance().getConnection();
System.out.println( conn.toString() );
}
}
考虑:通常,数据库、数据库名、数据库用户、密码等应该从配置文件中获取。因此,修改Oracle的工厂类:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
publicclass OracleDAOFactory {
privatestatic Properties prop = new Properties();
static {
try {
prop.load( OracleDAOFactory.class
.getResourceAsStream( "config.properties" ) );
} catch ( IOException e ) {
System.out.println( "File:config.properties no find,PLS check out!" );
e.printStackTrace();
}
}
private String CONNECTION_SERVER_NAME = prop
.getProperty( "oracle_server_name" );
private String CONNECTION_DRIVER = prop.getProperty( "oracle_conn_driver" );
private String CONNECTION_DBINSTANCE = prop
.getProperty( "oracle_dbInstance" );
private String CONNECTION_USER = prop.getProperty( "oracle_conn_user" );
private String CONNECTION_PWD = prop.getProperty( "oracle_conn_pwd" );
private String CONNECTION_URL = "jdbc:oracle:thin:@"
+ CONNECTION_SERVER_NAME + ":1521:" + CONNECTION_DBINSTANCE;
[1]