码迷,mamicode.com
首页 > 其他好文 > 详细

DAO模式

时间:2017-03-12 01:12:50      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:aof   oid   final   val   except   image   数据源   ima   auth   

技术分享

技术分享

 

 以下是完整的DAO模块  以及使用cache和JNDI访问数据源

 

 

package app10d.dao;
import java.sql.Connection;

//实际的一个DAO  实际的DAO都应该继承
public interface DAO {
    Connection getConnection() throws DAOException;

}
package app10d.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;

/**
 * 提供关闭和获取链接操作
 * @author 黄二狗
 *
 */
public class BaseDAO implements DAO {
    
    public Connection getConnection() throws DAOException {        
        DataSource dataSource = DataSourceCache.getInstance().getDataSource();
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException();
        }
    }

    protected void closeDBObjects(ResultSet resultSet, Statement statement, 
            Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}
package app10d.dao;

public class DAOException extends Exception {
    private static final long serialVersionUID = 19192L;

    public DAOException() {
    }
    public DAOException(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    private String message;
    
    public String toString() {
        return message;
    }
    
}
package app10d.dao;

import java.util.List;

import app10d.model.Product;

public interface ProductDAO extends DAO {
    List<Product> getProducts() throws DAOException;
    void insert(Product product) throws DAOException;

}
package app10d.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import app10d.model.Product;

public class ProductDAOImpl extends BaseDAO implements ProductDAO {
    
    private static final String GET_PRODUCTS_SQL = 
            "SELECT name, description, price FROM products";
    public List<Product> getProducts() throws DAOException {
        List<Product> products = new ArrayList<Product>();
        Connection connection = null;
        PreparedStatement pStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            pStatement = connection.prepareStatement(
                    GET_PRODUCTS_SQL);
            resultSet = pStatement.executeQuery();
            while (resultSet.next()) {
                Product product = new Product();
                product.setName(resultSet.getString("name"));
                product.setDescription(
                        resultSet.getString("description"));
                product.setPrice(resultSet.getFloat("price"));
                products.add(product);
            }
        } catch (SQLException e) {
            throw new DAOException("Error getting products. "
                    + e.getMessage());
        } finally {
            closeDBObjects(resultSet, pStatement, connection);
        }
        return products;
    }
    
    private static final String INSERT_PRODUCT_SQL = 
            "INSERT INTO products " +
            "(name, description, price) " +
            "VALUES (?, ?, ?)";
            
    public void insert(Product product) 
            throws DAOException {
        Connection connection = null;
        PreparedStatement pStatement = null;
        try {
            connection = getConnection();
            pStatement = connection.prepareStatement(
                    INSERT_PRODUCT_SQL);
            pStatement.setString(1, product.getName());
            pStatement.setString(2, 
                    product.getDescription());
            pStatement.setFloat(3, product.getPrice());
            pStatement.execute();
        } catch (SQLException e) {
            throw new DAOException("Error adding product. "
                    + e.getMessage());
        } finally {
            closeDBObjects(null, pStatement, connection);
        }
    }
}
package app10d.dao;

//
public class DAOFactory {
    public static ProductDAO getProductDAO() {
        return new ProductDAOImpl(); 
    }
}
package app10d.dao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceCache {
    private static DataSourceCache instance;
    private DataSource dataSource;
    static {
        instance = new DataSourceCache();
    }
    
    private DataSourceCache() {
        Context context = null;
        try {
            context = new InitialContext();
               dataSource = (DataSource) context.lookup(
                       "java:comp/env/jdbc/bean");
        } catch (NamingException e) {
        }
    }
    
    public static DataSourceCache getInstance() {
        return instance;
    }
    
    public DataSource getDataSource() {
        return dataSource;
    }

}

 

DAO模式

标签:aof   oid   final   val   except   image   数据源   ima   auth   

原文地址:http://www.cnblogs.com/HJL085/p/6536797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!