Druid阿里巴巴屬性文件 driverClass=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=UTC&character=utf8 user=root password=root #配置Druid鏈接池參數 initialSize=5 minIdle=3 maxActive=10 maxWait=60000 timeBetweenEvictionRunsMillis=2000 //阿里 Druid 鏈接池 import com.alibaba.druid.pool.DruidDataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JdbcUtils3 { //建立阿里巴巴鏈接池對象 private static DruidDataSource ds; private static Properties P; static { ds=new DruidDataSource(); P=new Properties(); //讀取屬性文件 InputStream input=Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties"); //加載P對象 try{ P.load(input); }catch(IOException ce){ ce.printStackTrace(); } //根據鍵獲取值 ds.setDriverClassName(P.getProperty("driverClass")); ds.setUrl(P.getProperty("url")); ds.setUsername(P.getProperty("user")); ds.setPassword(P.getProperty("password")); //配鏈接池的參數 ds.setInitialSize(Integer.parseInt(P.getProperty("initialSize"))); ds.setMinIdle(Integer.parseInt(P.getProperty("minIdle"))); ds.setMaxActive(Integer.parseInt(P.getProperty("maxActive"))); ds.setMaxWait(Integer.parseInt(P.getProperty("maxWait"))); ds.setTimeBetweenEvictionRunsMillis(Integer.parseInt(P.getProperty("timeBetweenEvictionRunsMillis"))); } //獲取數據庫鏈接對象 public static Connection getConnection()throws SQLException{ return ds.getConnection(); } //關閉數據庫鏈接對象之insert delete update的操做 public static void close(Connection con, Statement state)throws SQLException{ con.close();; state.close(); } //關閉數據庫鏈接的對象之 select 查找查詢的操做 public static void close(Connection con, Statement state, ResultSet set)throws SQLException{ set.close(); state.close(); con.close(); } //關閉獲取數據庫鏈接對象 public static void close(Connection con)throws SQLException{ con.close(); } // 關閉執行Statement執行SQL 語句的對象 public static void close(Statement state)throws SQLException{ state.close(); } //關閉結果集對象ResultSet對象 public static void close(ResultSet set)throws SQLException{ set.close(); } }