数据库连接池

DBCP连接池

DBCP数据库连接池的特点:
  开源的连接池,tomcat内置的连接池。
  极为高效的连接池。(DBCP连接池一秒钟可以创建并传递10万个左右的连接对象。)
  安全性并不高,有可能在高速运转丢失连接数据 (项目:对效率要求高,数据安全性方面要求低)
数据库连接池的使用步骤(先导入mysql数据连接的jar包mysql-connector-java-5.1.39-bin.jar)
1、导入jar包 commons-dbcp2-2.7.0.jar、commons-logging-1.2.jar(日志框架)
2、导入配置文件dbcpconfig.properties(配置文件建议放在src下)
代码部分
3、创建properties对象
4、读入配置文件(通过输入流)
5、创建连接池对象
6、从连接池中获取对象

public class Demo {
	public static void main(String[] args) throws Exception {
		//创建Properties对象
		Properties properties = new Properties();
		//读入配置文件(通过输入流)
		InputStream in = Demo.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");//此处是配置文件路径,因为在src下,直接写就行
		properties.load(in);
		//创建连接池对象
		DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
		//从连接池中获取对象
		Connection con = dataSource.getConnection();
		System.out.println(con);
	}
}

将上述过程封装成DbcpUtils

public class DbcpUtils {
	
	static DataSource ds = null;
	static {
		Properties properties = new Properties();
		//读取配置文件
		InputStream in = DbcpUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			//创建数据库连接池对象
			ds = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//从连接池中获取对象的方法
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}

测试工具类

@Test
	public void test2() throws SQLException {
		Connection con = DbcpUtils.getConnection();
		String sql = "insert into emp (id,ename) values(?,?)";
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setInt(1, 1016);
		pre.setString(2, "铁扇公主");
		int result = pre.executeUpdate();
		if (result>0) {
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
		//把连接归还给连接池
		con.close();
	}

C3p0连接池

C3p0数据库连接池的特点:
  开源免费
  效率比dbcp低,但安全性更高
数据库连接池的使用步骤(先导入mysql数据连接的jar包mysql-connector-java-5.1.39-bin.jar)
1、导入jar包 c3p0-0.9.2-pre5.jar mchange-commons-java-0.2.3.jar
2、导入配置文件 c3p0-config.xml
注意c3p0的文件名、配置文件位置都是固定,不能更改

public static void main(String[] args) throws SQLException {
		//创建连接池对象
		//默认配置
		DataSource ds = new ComboPooledDataSource();
		//命名的配置
		//DataSource ds = new ComboPooledDataSource("offcn");
		System.out.println(ds.getConnection());
	}

C3p0数据库连接池的使用方法比dbcp要简单,,直接创建一个数据库连接池对象,然后通过数据库连接池对象拿到连接对象,不用再去读取配置文件,因为C3p0是自动去src下找c3p0-config.xml文件去加载,所以说配置文件的名字和位置都不能改变
此外需要注意的是C3p0也提供了命名的配置,这可以使我们在同一个配置文件里配置两个数据源,我们可以根据需要选择合适的数据源
封装工具类C3p0Utils

public class C3p0Utils {
	static DataSource ds = null;
	static {
		 ds = new ComboPooledDataSource();
	}
	//获取连接
	public static  Connection getConn() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}

测试工具类

@Test
	public void test() throws SQLException {
		Connection conn = C3p0Utils.getConn();
		String sql = "insert into emp (id,ename) values(?,?)";
		PreparedStatement pre = conn.prepareStatement(sql);
		pre.setInt(1, 1017);
		pre.setString(2, "铁扇公主");
		int result = pre.executeUpdate();
		if (result>0) {
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
		//归还连接池
		conn.close();
	}

Druid连接池

Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验
数据库连接池的使用步骤
1、导入jar包
2、导入配置文件(配置文件建议放在src下)
代码部分
3、创建properties对象
4、读入配置文件(通过输入流)
5、创建连接池对象
6、从连接池中获取对象
和Dbcp数据库连接池的使用方法极其相似

public class DruidUtils {
	static DataSource ds = null;
	static {
		//读取配置文件
		Properties properties = new Properties();
		InputStream in = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//创建数据库连接池对象
		try {
			ds = DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//获取数据库连接池中的连接对象
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值