如何通过读取DBCP配置文件来实现连接数据库

 用到的jar包:

mysql-connector-java-8.0.18.jar
commons-dbcp-1.4.jar
commons-pool-1.6.jar
commons-logging-1.2.jar

DBCP配置文件:注意:配置文件名称必须为dbcpconfig.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/chapter02?useSSL=false&serverTimezone=UTC
username=root
password=1111

initialSize=10

maxActive=50

maxIdle=20

minIdle=5

maxWait=60000

TestDBCP代码:

package test.c3p0;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class TestDBCP_01 {

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/chapter02?useSSL=false&serverTimezone=UTC");
		dataSource.setUsername("root");
		dataSource.setPassword("1111");
		
		Connection conn = dataSource.getConnection();
		System.out.println(conn);
	}
}

实现效果:

发布了38 篇原创文章 · 获赞 9 · 访问量 1440

猜你喜欢

转载自blog.csdn.net/qq_42023080/article/details/104741998