DAO工具类的封装源码

详细源码见下表,绝对原创,转载请注明出处!
package com.ydj.util;

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

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;


public class DAOUtil {
    
    private static DataSource source = null;
    
    static {
        try {
            Properties p = new Properties();
            p.load(DAOUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
            source = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 实现与数据库的连接
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = source.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    
    /**
     * 实现数据库的关闭
     */
    public static void close(ResultSet set, PreparedStatement ps, Connection conn) {
        try {
            if(set != null) set.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(ps != null) ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if(conn != null) conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 实现数据库表增删改的工具类
     */
    public static int executeUpdate(String sql,Object...data) {
        
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DAOUtil.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i+1, data[i]);
            }
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DAOUtil.close(null, ps, conn);
        }
        return 0;
    }
    
    /**
     * 实现数据库表查询的工具类
     */
    public static List> executeQuery(String sql,Object...data){
        
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet set = null;
        try {
            conn = DAOUtil.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i+1, data[i]);
            }
            set = ps.executeQuery();
            List> list = new ArrayList<>();
            while(set.next()) {
                int i = 1;
                Map map = new HashMap<>();
                while(i<=set.getMetaData().getColumnCount()) {
                    map.put(set.getMetaData().getColumnLabel(i), set.getObject(i));
                    i++;
                }
                list.add(map);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DAOUtil.close(set, ps, conn);
        }
        return null;
    }
    
}

 

你可能感兴趣的:(DAO工具类的封装源码)