記錄Druid 鏈接池簡單用法java
package Utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; 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 Druid { private static DruidDataSource dataSource; static { try {
dataSource.setUrl();
dataSource.setDriverClassName();
dataSource.setUsername();
dataSource.setPassword();
//設置初始化創建物理鏈接個數,最小鏈接池數量,最大鏈接池數量
dataSource.setInitalSize(6);
dataSource.setMinIdle(5);
dataSource.setMaxActive(10);
//匹配獲取鏈接等待超時時間
dataSource.setMaxWait(30000);sql
InputStream inputStream = DBCP.class.getClassLoader().getResourceAsStream("dbconfig.properties"); Properties properties = new Properties(); properties.load(inputStream); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void release(Connection conn, Statement st, ResultSet rs) { if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (Exception e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } } }