一.连接池
InputStream inputStream = null;try {// 1.读取 db.properties 配置文件的内容inputStream= DBUtil.class.getClassLoader().getResourceAsStream("db.properties");// 2.创建 Properties 类的对象Properties prop = new Properties();prop.load(inputStream);// 3.初始化数据库连接池dataSource = DruidDataSourceFactory.createDataSource(prop);//**********************//在外部文件中// 4.从数据源获得连接dataSource.getConnection();//**********************} catch (Exception e) {e.printStackTrace();} finally {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}
db.properties
url=jdbc:mysql://域名/数据库?serverTimezone=Asia/Shanghaiusername=用户名password=密码# 最大连接池数量maxActive=10# 最小连接池数量minIdle=1
