DAO 도구 클래스의 패키지된 소스 코드

10099 단어
       ,    ,       !
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;
    }
    
}

좋은 웹페이지 즐겨찾기