DataSource:数据源,包含连接池和连接池管理两部分,用来取代DriverManager获取连接,获取速度快,同时可以大幅度提供商数据库访问速度
初体验:
@Test public void test1() throws Exception { ComboPooledDataSource cpds = new ComboPooledDataSource(); //四大参数 cpds.setDriverClass("com.mysql.cj.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true"); cpds.setUser("root"); cpds.setPassword("123456"); cpds.setInitialPoolSize(10);//初始数据库连接池中的连接数 Connection connection = cpds.getConnection(); System.out.println(connection); DataSources.destroy(cpds);//销毁c3po连接池,一般不用 connection.close(); cpds.close(); }使用配置文件:
@Test public void test2() throws Exception{ ComboPooledDataSource cpds = new ComboPooledDataSource(); Connection connection = cpds.getConnection(); System.out.println(connection); connection.close(); cpds.close(); }配置文件示例:
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 这是默认配置信息 --> <default-config> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true</property> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="user">root</property> <property name="password">123456</property> <!-- 池参数配置 --> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <!-- 专门为oracle提供的配置信息 --> <named-config name="oracle-config"> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true</property> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="user">root</property> <property name="password">123456</property> <!-- 池参数配置 --> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>初体验:
@Test public void test1() throws Exception{ //创建DBCP数据库连接池 BasicDataSource ds = new BasicDataSource(); //设置连接参数 ds.setUsername("root"); ds.setPassword("123456"); ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true"); //设置其他属性等等 ds.setInitialSize(1); Connection connection = ds.getConnection(); System.out.println("连接:"+connection.getClass()); connection.close(); ds.close(); }使用配置文件:
@Test public void test2() throws Exception{ Properties properties = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbconfig.properties"); properties.load(is); BasicDataSource ds = BasicDataSourceFactory.createDataSource(properties); Connection connection = ds.getConnection(); System.out.println(connection.getClass()); is.close(); connection.close(); ds.close(); }配置文件示例:
driverClassName=com.mysql.cj.jdbc.Driver username=root password=123456 url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&rewriteBatchedStatements=trueDruid可以说是目前最好的连接池了,也是我们最常用的. 配合配置文件使用:
@Test public void test1() throws Exception { Properties properties = new Properties(); FileInputStream is = new FileInputStream(new File("src/main/resources/dbconfig.properties")); properties.load(is); DataSource dds = DruidDataSourceFactory.createDataSource(properties); Connection connection = dds.getConnection(); System.out.println(connection); connection.close(); }