记录Druid 连接池简单用法
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);
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(); } } } }