标签:nts druid factor utils connect exce put out http
Druid 阿里提供的数据库连接池,集以上连接池优点于一身,开发使用此连接池
使用配置文件方式获取Druid数据库连接池
TestDruid
package com.aff.connection; import java.io.InputStream; import java.sql.Connection; import java.util.Properties; import javax.sql.DataSource; import org.junit.Test; import com.alibaba.druid.pool.DruidDataSourceFactory; //使用配置文件方式获取Druid数据库连接池 public class TestDruid { @Test public void getConnection() throws Exception { Properties pros = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties"); pros.load(is); DataSource source = DruidDataSourceFactory.createDataSource(pros); Connection conn = source.getConnection(); System.out.println(conn); } }
使用Druid数据连接池获取连接
JDBCUtilsDruid
package com.aff.util; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; //使用Druid数据连接池获取连接 public class JDBCUtilsDruid { private static DataSource source; static { try { Properties pros = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties"); pros.load(is); source = DruidDataSourceFactory.createDataSource(pros); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection conn = source.getConnection(); return conn; } }
测试Druid数据库连接池获取的连接的使用
@Test public void testGetCount() { Connection conn = null; try { conn = JDBCUtilsDruid.getConnection(); Long count = dao.getCount(conn); System.out.println(count); System.out.println("获取成功"); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn, null); } }
目录结构
标签:nts druid factor utils connect exce put out http
原文地址:https://www.cnblogs.com/afangfang/p/12684709.html