详细代码请参见 https://code.csdn.net/jediael_lu/daopattern
1、创建Dao接口。
package com.ljh.jasonnews.server.dao;
import java.sql.Connection;
public interface Dao {
public Connection getConnection() throws DaoException;
}
2、创建BaseDao类,实现Dao接口,主要完成数据库的打开与关闭
package com.ljh.jasonnews.server.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DaoBase implements Dao {
@Override
public Connection getConnection() throws DaoException {
// DataSource dataSource = DataSourceCache.getInstance().getDataSource();
try {
//注册JDBC驱动程序
Class.forName("oracle.jdbc.OracleDriver");
//打开一个数据库连接
String URL = "jdbc:oracle:thin:@172.16.80.155:1521:nfirms";
String USERNAME = "geeknews";
String PASSWORD = "Lu123456";
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
//return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new DaoException();
}
}
protected void closeDbObject(ResultSet rs, Statement stmt, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3、创建DaoException。
package com.ljh.jasonnews.server.dao;
public class DaoException extends Exception{
private String message;
public DaoException(){}
public DaoException(String message){
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String toString(){
return message;
}
}
以上为jdbc DAO模式的基本步骤,主要用于获取连接及异常处理。
以下步骤对于每个表均要进行新增类(***Dao,***DaoImpl,model.***)或者在类中新增方法(DaoFactory)。4、创建DaoFactory类,用于生产Dao对象。
package com.ljh.jasonnews.server.dao.factory;
import com.ljh.jasonnews.server.dao.CategoryDao;
import com.ljh.jasonnews.server.dao.impl.CategoryDaoImpl;
public class DaoFactory {
public static CategoryDao getCategoryDao() {
return new CategoryDaoImpl();
}
}
5、创建Model类。
package com.ljh.jasonnews.server.model;
public class Category {
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getSequnce() {
return sequnce;
}
public void setSequnce(int sequnce) {
this.sequnce = sequnce;
}
public int getDeleted() {
return deleted;
}
public void setDeleted(int deleted) {
this.deleted = deleted;
}
private int cid;
private String title;
private int sequnce = 0;
private int deleted = 0;
}
6、创建***Dao接口,继承Dao接口。
package com.ljh.jasonnews.server.dao;
import java.util.List;
import com.ljh.jasonnews.server.model.Category;
public interface CategoryDao extends Dao{
public List<Category> getCategoryList() throws DaoException;
}
7、创建***DaoImpl类,继承DaoBase类。
package com.ljh.jasonnews.server.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.ljh.jasonnews.server.dao.CategoryDao;
import com.ljh.jasonnews.server.dao.DaoBase;
import com.ljh.jasonnews.server.dao.DaoException;
import com.ljh.jasonnews.server.model.Category;
public class CategoryDaoImpl extends DaoBase implements CategoryDao {
@Override
public List<Category> getCategoryList() throws DaoException{
String GET_CATEGORY_SQL = "SELECT * FROM T_CATEGORY";
List<Category> categoryList = new ArrayList<Category>();
Connection conn = null;
PreparedStatement pStatment =null;
ResultSet rs = null;
try{
conn = getConnection();
System.out.println("a");
pStatment = conn.prepareStatement(GET_CATEGORY_SQL);
System.out.println("b");
rs = pStatment.executeQuery();
System.out.println("c");
while(rs.next()){
Category category = new Category();
category.setCid(rs.getInt("cid"));
category.setTitle(rs.getString("title"));
category.setSequnce(rs.getInt("sequnce"));
category.setDeleted(rs.getInt("deleted"));
categoryList.add(category);
}
}catch(Exception e){
throw new DaoException("Erorr getting Categorys. " + e.getMessage());
}finally{
closeDbObject(rs, pStatment, conn);
}
return categoryList;
}
}
其它说明:
1、创建TestCase,测试数据库连接。
package com.ljh.jasonnews.server.dao.test;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import com.ljh.jasonnews.server.dao.CategoryDao;
import com.ljh.jasonnews.server.dao.impl.CategoryDaoImpl;
import com.ljh.jasonnews.server.model.Category;
public class CategoryDaoTest {
@Test
public void test() throws Exception{
CategoryDao categoryDao = new CategoryDaoImpl();
List<Category> categoryList = categoryDao.getCategoryList();
Iterator<Category> iterator = categoryList.iterator();
while(iterator.hasNext()){
Category category = iterator.next();
System.out.println(category.getCid()+" "+ category.getTitle()+" "+category.getSequnce()+" "+ category.getDeleted()+" ");
}
}
}
2、在数据库中访问数据,最重要且最费时的操作经常是建立连接。按规则,设计良好的应用程序数据库连接应该始终是采用连接池的。
一般而言,使用连接池有以下三种方法:
l Apache Commons DBCP
l C3p0
l Tomcat7中的Tomcat JDBCConnection Pool
使用Tomcat的项目,建立直接使用TomcatJDBC Connection Pool。调用DataSource.getConnection()方法比较快,因为连接永远不会被关闭:关闭连接时,只要将连接返回池中即可。但是,JNDI查找比较慢,因此,被返回的DataSource经常会被缓存起来。注:
(1)在调试中,未能使用连接池完成数据库连接,因此本示例中未使用连接池,关于连接池,可参考DataSourceCache.java,但关键是context.xml与web.xml中的配置。
(2)在需要调用context相关的应用中,不能直接使用junit进行测试,而必须创建一个jsp或者servlet,否则,在以下代码中会报错:
Context envContext = (Context)context.lookup("java:/comp/env");(3)作用连接池有JNDI及依赖注入2种方式,目前更推荐使用依赖注入。
之后再补充关于连接池以及缓存相关的代码。