1、创建一个数据源对象
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
2、通过配置文件获取相关信息
Properties properties = new Properties();
properties.load(new FileReader("src\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
3、给数据源设置相关参数
comboPooledDataSource.setDriverClass(driver); comboPooledDataSource.setJdbcUrl(url); comboPooledDataSource.setUser(user); comboPooledDataSource.setPassword(password);
4、设置初始化连接数和最大连接数
comboPooledDataSource.setInitialPoolSize(10);//代表刚开始连接池中有10个连接
comboPooledDataSource.setMaxPoolSize(50);//表示连接池中的连接可以随进程数的增多增加到50个
5、获取连接
Connection connection = comboPooledDataSource.getConnection();
此时就已经连接到指定的数据库
需要先将必要信息加入到配置文件当中
user=root password=123456 url=jdbc:mysql://localhost:3306/books?rewriteBatchedStatements=true&serverTimezone=UTC driver=com.mysql.cj.jdbc.Driver



