#druid.properties配置文件 通过读取外部配置文件的方法,方法实例化druid连接池对象
# key = value => java Properties (key | value)
# durid配置的key固定命名
#edruid连接池需要的配置參数,Key固定命名
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=12345678
url=jdbc:mysql:
package com.shayiheng.api.druid;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DruidUsePart {@Testpublic void testHard() throws SQLException {DruidDataSource dataSource=new DruidDataSource();dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/shayiheng");dataSource.setUsername("root");dataSource.setPassword("12345678");dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setInitialSize(5);dataSource.setMaxActive(10);Connection connection = dataSource.getConnection();connection.close();}@Testpublic void testSoft() throws Exception {Properties properties=new Properties();InputStream resourceAsStream = DruidUsePart.class.getClassLoader().getResourceAsStream("druid.properties");properties.load(resourceAsStream);DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);Connection connection=dataSource.getConnection();connection.close();}
}