JDBC从连接池获取连接(Druid连接池)

package com.gotion.dataextract.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.gotion.common.core.constant.Constants;
import com.gotion.common.core.constant.ServiceNameConstants;
import com.gotion.common.core.exception.BaseException;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库链接工具类
 *
 * @author fangzp
 * @date 2022-06-16
 */
public class JdbcUtil {

    /**
     * 从druid数据库连接池获取数据库连接
     *
     * @param driver   数据库驱动
     * @param url      数据库连接串
     * @param username 用户名
     * @param password 密码
     * @return 连接
     */
    public static Connection druidDataSource(String driver, String url, String username, String password) {
        try {
            Properties properties = new Properties();
            properties.setProperty("driverClassName", driver);
            properties.setProperty("url", url);
            properties.setProperty("username", username);
            properties.setProperty("password", password);
            properties.setProperty("initialSize", "10");
            properties.setProperty("maxActive", "20");
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new BaseException(ServiceNameConstants.DATA_SERVICE, Constants.ERROR, null, "获取数据库连接错误");
        }
    }

    /**
     * 释放连接资源
     *
     * @param conn 连接
     * @param ps   预处理对象
     * @param rs   结果集
     */
    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Druid是阿里巴巴开源的一个数据库连接,它具有连接的基本功能,同时还提供了监控、防御SQL注入攻击、缓存等高级功能。下面是使用Druid连接进行JDBC操作的步骤: 1. 引入Druid的依赖 ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 2. 配置Druid连接 可以通过properties文件、xml文件或者代码配置Druid连接,这里以代码配置为例: ``` import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.SQLException; public class DruidUtils { private static DruidDataSource dataSource = null; static { dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } ``` 3. 使用Druid连接获取数据库连接 ``` Connection conn = DruidUtils.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE id = ?"); ps.setInt(1, 1); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString("name")); } rs.close(); ps.close(); conn.close(); ``` 4. 关闭连接 使用完数据库连接后,需要关闭连接,释放资源。 ``` if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } ``` 以上就是使用Druid连接进行JDBC操作的基本步骤,可以有效提高数据库连接的使用效率和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方大拿拿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值