java利用druid手动创建datasource

package com.deepthinking.bi.sjzz.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

//连接池的工具类
public class DataSourceUtils {
    
    // 声明变量 static
    public static DataSource dataSource;

    public DataSource createdDataSource(String drive, String url, String username,
                                        String password, String datasourceName) throws Exception {
        Map map = new HashMap<>();
        map.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, drive);
        map.put(DruidDataSourceFactory.PROP_URL, url);
        map.put(DruidDataSourceFactory.PROP_USERNAME, username);
        map.put(DruidDataSourceFactory.PROP_PASSWORD, password);
        // 初始化时建立物理连接的个数
        map.put(DruidDataSourceFactory.PROP_INITIALSIZE, "10");
        // 最小连接池数量
        map.put(DruidDataSourceFactory.PROP_MINIDLE, "10");
        // 最大连接池数量
        map.put(DruidDataSourceFactory.PROP_MAXACTIVE, "50");
        // 获取连接时最大等待时间,单位毫秒
        map.put(DruidDataSourceFactory.PROP_MAXWAIT, "60000");
        // 检测连接的间隔时间,单位毫秒
        map.put(DruidDataSourceFactory.PROP_TIMEBETWEENEVICTIONRUNSMILLIS, "60000");
        // wall:防御sql注入  stat:监控统计
        map.put(DruidDataSourceFactory.PROP_FILTERS, "wall,stat");

        map.put(DruidDataSourceFactory.PROP_NAME, datasourceName);

        return DruidDataSourceFactory.createDataSource(map);
    }


    // 提供获取连接池的方法
    public static DataSource getDataSource() {
        return dataSource;
    }

    // 提供获取连接的方法
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    // 提供关闭资源的方法【connection是归还到连接池】
    // 提供关闭资源的方法 【方法重载】3 dql
    public static void closeResource(ResultSet resultSet, Statement statement, Connection connection) {
        // 关闭结果集
        // ctrl+alt+m 将java语句抽取成方法
        closeResultSet(resultSet);
        // 关闭语句执行者
        closeStatement(statement);
        // 关闭连接
        closeConnection(connection);
    }

    // 提供关闭资源的方法 【方法重载】 2 dml
    public static void closeResource(Statement statement, Connection connection) {
        // 关闭语句执行者
        closeStatement(statement);
        // 关闭连接
        closeConnection(connection);
    }

    private static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void closeResultSet(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

使用

ds=DataSourceUtils.dataSource

 //获取连接

  Connection conn = ds.getConnection();

public static List> listMapObejct(DataSourceUtils dataSourceUtils, String limitSql) throws SQLException {
        List> listmap = new ArrayList<>();
        Map map = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = dataSourceUtils.dataSource.getConnection();
            //通过工具类获取数据库连接对象
            ps = con.prepareStatement(limitSql);
            rs = ps.executeQuery();
            int columnCount = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                map = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    map.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
                }
                listmap.add(map);
            }
            return listmap;
        } finally {
            dataSourceUtils.closeResource(rs,ps, con);
        }
    }

你可能感兴趣的:(java)