配置文件
使用properties文件配置相關數據
--driverClassName= com.mysql.cj.jdbc.Driver 驅動加載
--username=root 連接數據庫的用戶名
--password= 連接數據庫的密碼
--url=jdbc:mysql://127.0.0.1:3306/庫名?characterEncoding=utf-8 注冊驅動
--initialSize= 初始化池中建立的物理連接個數
--maxActive= 最大可活躍連接池的數量
導包
在lib目錄下導入包:
druid-1.1.5.jar mysql-connector-java-5.1.41-bin.jar
數據庫連接
private static DataSource source;
static {
try {
Properties properties = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Druid.properties");
properties.load(is);
source = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection connection = source.getConnection();
return connection;
}

