JavaJDBC-使用数据库连接池技术(Druid)获取Connection对象

一、导入对应的jar包

 

二、创建对应的配置文件(如下只是部分参数)

url=jdbc:mysql://localhost:3306/test
username=root
password=orz*****
driverClassName=com.mysql.cj.jdbc.Driver

initialSize=10
maxActive=20

 

三、获取Connection对象

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(); } }
//getConnection
public static Connection getConnectionByDruid()throws Exception { Connection conn=source.getConnection(); return conn; }

 

posted @ 2020-08-04 22:52  orz江小鱼  阅读(1771)  评论(0编辑  收藏  举报