手寫數(shù)據(jù)庫連接池-C++11代碼實戰(zhàn)經(jīng)典-多線程和智能指針
? public static void test01() throws Exception{
? ? ? ?// 1. 創(chuàng)建一個Properties對象,讓其去讀取druid.properties文件
? ? ? ?Properties properties = new Properties();
? ? ? ?// 1.1 將druid.properties配置文件轉(zhuǎn)成字節(jié)輸入流// ? ? ? ?FileInputStream is = new FileInputStream("絕對路徑");
? ? ? ?// 1.2 使用相對路徑來將配置文件轉(zhuǎn)成字節(jié)輸入流,可以使用類加載器來讀取類路徑下文件
? ? ? ?// 獲取ClassLoader對象
? ? ? ?ClassLoader classLoader = TestDataSource.class.getClassLoader();
? ? ? ?InputStream is = classLoader.getResourceAsStream("druid.properties");
? ? ? ?// 1.2 使用properties對象加載流
? ? ? ?properties.load(is);
? ? ? ?// 2. 使用創(chuàng)建DruidDataSourceFactory創(chuàng)建Druid的連接池對象
? ? ? ?DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
? ? ? ?// 3. 從連接池中取出一個連接使用
? ? ? ?Connection connection1 = dataSource.getConnection();