jdbc-durid连接池

目录

  1. 项目结构
  2. druid.properties文件配置
  3. 书写JDBCUtil工具类
  4. 数据库连接测试

这只是一个简单的durid的Demo,不包含spring和后台监控!!!

项目结构

在这里插入图片描述
这里mysql的jar包和durid的两个用户库必须存在,否则不会成功。
durid的jar包和简单配置文件大家可以去我的百度云盘下载。

druid.properties文件配置

druid.properties文件放在src目录下,名字可以任意。
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
#初始的连接数
initialSize=3
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000
#最小空闲连接数
minIdle=2

书写JDBCUtil工具类

package utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
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;

/**
 * @author jfr 
 * Druid连接池的工具类
 */
public class JDBCUtil_druid {
	private static DataSource ds;
	static {

		try {
			// 加载配置文件
			Properties pro = new Properties();
			pro.load(JDBCUtil_druid.class.getClassLoader().getResourceAsStream("druid.properties"));
			ds = DruidDataSourceFactory.createDataSource(pro);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

// 获取连接池
	public static DataSource getDataSource() {
		return ds;
	}

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

//归还连接
	public static void close(Statement stmt, Connection conn) {
		close(null, stmt, conn);
	}

	public static void close(ResultSet rs, Statement stmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public static void close(PreparedStatement pstmt, Connection conn) {
		close(null, pstmt, conn);
	}

	public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

数据库连接测试

Demo1:

package druid;

import java.sql.Connection;
import utils.JDBCUtil_druid;

/**
 * @author jfr
 *
 */
public class druidDemo {
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		for (int i = 0; i <= 10; i++) {
			Connection conn = JDBCUtil_druid.getConnection();
			System.out.println(i + ":" + conn);
			if (i == 3) {
				conn.close();
			}
		}

	}
}

在这里插入图片描述
出现上图最上方那些信息说明,证明我们的durid配置成功。

这是一个简单的测试,用来获取每一个连接的信息,其中红框圈出来的值相同,
我们以前调用close()方法是关闭了连接,而在线程池中close()方法是把使用完的连接归还给连接池以便下一个申请调用。

Demo2:

package druid;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import utils.JDBCUtil_druid;

public class druid_select_test {
	public static void main(String[] args) throws Exception {
		Connection conn = null;
		Statement stmt = null;
		String id = null;
		String name = null;
		String sex = null;

		conn = JDBCUtil_druid.getConnection();

		stmt = conn.createStatement();
		String sql = "select * from user";
		ResultSet rs = stmt.executeQuery(sql);
		// 表查询
		while (rs.next()) {
			id = rs.getString("id");
			name = rs.getString("username");
			sex = rs.getString("sex");

			if (id == null) {
				System.out.println("表为空!");
			} else {
				System.out.println("id = " + id + "," + "name = " + name + ", " + "sex= " + sex);
			}

		}
	}
}

在这里插入图片描述
这是一个基于durid的简单查表,使用JDBCUtil工具类进行durid的连接并测试查表。

不包含spring和后台访问监控!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值