JDBC:Druid连接池

Druid连接池的使用


环境:eclipse+mysql80

一、下载工具包

  1. druid工具包:https://pan.baidu.com/s/1KogYNcytyuelN4tu0wkPiA   提取码:w0mg 
  2. mysql驱动包:https://dev.mysql.com/downloads/connector/j

 

二、导入包

在根目录新建 libs 文件夹 (文件夹随意)

将两个驱动包复制进来(mysql驱动包 + druid-1.0.9.jar)

然后将两个包添加到构建路径即:右键 jar 包 ==> build path ==> add to build path

 

三、创建或导入 配置文件 druid.properties

在 src 目录下(别的目录也可以,因为下面需要手动加载它)新建一个名为 druid.properties 的文件

或 将在上面链接下载的druid工具包中的 druid.properties 导入

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai
username=用户名
password=密码
initialSize=5
maxActive=10
maxWait=3000

 

四、创建 JdbcUtils 工具类

JdbcUtils.java

package com.health.datasource.druid.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * Druid连接池的工具类
 */
public class JdbcUtils {
	//1.定义成员变量  DataSource
	private static DataSource ds;
	static {
		try {
			//1.1加载配置文件
			Properties pro = new Properties();
			pro.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
			//2.获取DataSource
			ds = DruidDataSourceFactory.createDataSource(pro);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 获取连接
	 * @throws SQLException 
	 */
	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}
	
	/**
	 * 释放资源
	 */
	public static void close(Statement stmt,Connection conn) {
		close(stmt,conn,null);
	}
	public static void close(Statement stmt,Connection conn,ResultSet rs) {
		if(stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(rs!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取连接池方法
	 */
	public static DataSource getDataSource() {
		return ds;
	}
	
}

 

五、测试类

DruidTest.java

package com.health.datasource.druid.utils;

import java.sql.*;

/**
 * 使用工具类
 */
public class DruidTest {
	public static void main(String[] args) {
		/**
		 * 给account表添加一条记录
		 */
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			//1.获取连接
			conn = JdbcUtils.getConnection();
			//2.定义sql
			String sql = "insert into account values(3,?,?)";
			//3.获取pstmt对象
			pstmt = conn.prepareStatement(sql);
			//4.给?赋值
			pstmt.setString(1, "王五");
			pstmt.setString(2, "3000");
			//5.执行sql
			int count = pstmt.executeUpdate();
			System.out.println(count);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			//6.释放资源
			JdbcUtils.close(pstmt, conn);
		}
	}
}

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

healthLau

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

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

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

打赏作者

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

抵扣说明:

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

余额充值