栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

阿里的开源连接池框架druid的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阿里的开源连接池框架druid的使用

文章目录

Jdbc 的不足数据连接池(DBCP)数据源框架(druid)

使用流程 完整代码

Jdbc 的不足

jdbc:java和数据库的桥梁

步骤:

注册驱动 Class.forName()—>一次建立连接 Connection —>每一次预处理对象PreparedStatement

Statement对象 sql拼接—>SQL注入漏洞PreparedStatement对象

public interface PreparedStatement extends Statement 执行SQL关闭连接—>每一次
建立连接(消耗资源) 关闭连接(释放资源) 数据连接池(DBCP)

    定义好连接数从连接池查找是否有空闲连接使用空闲连接放回池子中
数据源框架(druid)

阿里巴巴的开源连接池框架

使用流程
    创建druid.properties配置文件
druid.driver = com.mysql.cj.jdbc.Driver
druid.url = jdbc:mysql://localhost:3306/mvc_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
druid.username=root
druid.password=1234
    使用Properties对象,读取配置文件
// 用来解析properties文件
	private static class DruidPropertiesConfig {

		void load() {
			try {
				// 得到配置文件的路径
				String path = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
				 String fileName = path + "com/dyit/vrius/resources/druid.properties";
				//String fileName = "D:\sts-workspace\vuris-sys\src\com\dyit\vrius\resources\druid.properties";

				Properties prop = new Properties();

				prop.load(new FileReader(fileName));

				JdbcUtil.prop = prop;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Logger.getLogger(this.getClass()).debug(e.getMessage());
			}

		}

	}
    引入druid.jar包(jar包放在文末),然后创建数据数据源对象DruidDataSource
// 私有化构造方法 采用双检模式来创建单例
	private JdbcUtil() {
		// 首先加载配置文件
		new DruidPropertiesConfig().load();
		// 得到数据源对象
		ds = new DruidDataSource();
		// 加载配置文件
		ds.configFromPropety(prop);
		Logger.getLogger(this.getClass()).debug("数据源配置成功");
	}
    建立连接
public static void connect() {
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			Logger.getLogger(JdbcUtil.class).debug(e.getMessage());
			e.printStackTrace();
		}

	}

jdbc–> 封装(static)–>properties(配置文件)–>数据源(dbcp)–>单例

完整代码
package com.dyit.vrius.util;


import java.io.FileReader;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;

//创建单例模式
public class JdbcUtil {

	// 创建Propertise对象以便于创建数据源
	private static Properties prop;
	// 私有化类实例 创建单例模式
	private static JdbcUtil instance = null;
	// 创建数据源
	private DruidDataSource ds;
	// 创建数据库链接对象
	private Connection conn;
	// 创建面板对象
	private PreparedStatement prep;
	// 创建resultset集合
	private ResultSet rs;

	// 用来解析properties文件
	private static class DruidPropertiesConfig {

		void load() {
			try {
				// 得到配置文件的路径
				String path = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
				 String fileName = path + "com/dyit/vrius/resources/druid.properties";
				//String fileName = "D:\sts-workspace\vuris-sys\src\com\dyit\vrius\resources\druid.properties";

				Properties prop = new Properties();

				prop.load(new FileReader(fileName));

				JdbcUtil.prop = prop;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Logger.getLogger(this.getClass()).debug(e.getMessage());
			}

		}

	}

	// 私有化构造方法 采用双检模式来创建单例
	private JdbcUtil() {
		// 首先加载配置文件
		new DruidPropertiesConfig().load();
		// 得到数据源对象
		ds = new DruidDataSource();
		// 加载配置文件
		ds.configFromPropety(prop);
		Logger.getLogger(this.getClass()).debug("数据源配置成功");
	}

	public static JdbcUtil getInstance() {
		if (instance == null) {
			synchronized (JdbcUtil.class) {
				if (instance == null) {
					instance = new JdbcUtil();
				}
			}
		}

		return instance;
	}

	// 建立链接
	public void connect() {
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			Logger.getLogger(this.getClass()).debug(e.getMessage());
			e.printStackTrace();
		}

	}

	// 得到preparedStatment面板
	public void preparedStatment(String sql, Object... vals) {
		try {
			prep = conn.prepareStatement(sql);
			for (int i = 0; i < vals.length; i++) {
				prep.setObject(i + 1, vals[i]);
			}
		} catch (SQLException e) {
			Logger.getLogger(this.getClass()).debug(e.getMessage());
			e.printStackTrace();
		}
	}

	// 执行语句
	public void executeUpdate() {
		try {
			prep.executeUpdate();
		} catch (SQLException e) {
			Logger.getLogger(this.getClass()).debug(e.getMessage());
			e.printStackTrace();
		}
	}

	// 查询语句
	public ResultSet executeQuery() {
		try {
			rs = prep.executeQuery();
		} catch (SQLException e) {
			Logger.getLogger(this.getClass()).debug(e.getMessage());
			e.printStackTrace();
		}
		return rs;
	}

	// 关闭链接
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (prep != null) {
				prep.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			Logger.getLogger(this.getClass()).debug(e.getMessage());
			e.printStackTrace();
		}
	}

}

jar包连接

链接:https://pan.baidu.com/s/1F0BVqHAd3ABJ9DijMLzGPw 
提取码:88u9
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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