栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

使用Druid连接池连接和关闭数据库

使用Druid连接池连接和关闭数据库

 使用Druid连接池连接数据库,首先先写配置资源,同时需要导druid-1.1.10.jar包

以及数据库驱动mysql-connector-java-8.0.15.jar包

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/表名称?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&rewriteBatchedStatements=true
username=数据库用户名
password=数据库密码

initialSize=10
maxActive=10

然后读取配置资源 ,加载驱动

public class DruidConn {

    private static DataSource dataSource;
    static {
        try {
            Properties pros = new Properties();
//读取文件配置资源
            InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("Druild.properties");
//加载驱动
            pros.load(resourceAsStream);

            dataSource = DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //使用druid连接数据库
    public static Connection getConnection3()  {

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            return connection;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
       return null;
    }
   
}

最后关闭数据库连接池

 public static void close(Connection connection, PreparedStatement preparedStatement){
        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(preparedStatement);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号