记性不如烂笔头21-JAVA数据库连接池DBCP(二)

2015-07-24 11:33:44 · 作者: · 浏览: 13
92.168.19.1:1521:fanfangming"); p.setProperty("password","ffm"); p.setProperty("username","ffm"); p.setProperty("maxActive","30"); p.setProperty("maxIdle","10"); p.setProperty("maxWait","1000"); p.setProperty("removeAbandoned","false"); p.setProperty("removeAbandonedTimeout", "120"); p.setProperty("testOnBorrow","true"); p.setProperty("logAbandoned","true"); dataSource =(BasicDataSource)BasicDataSourceFactory .createDataSource(p); }catch(Exception e) { e.printStackTrace(); } } public static synchronized ConnectiongetConnection() throwsSQLException{ if (dataSource == null) { init(); } Connectionconn = null; if (dataSource != null) { conn= dataSource.getConnection(); } return conn; } public static void main(String[] args) throws Exception { Connectioncon = null; try { con= DbcpUsage.getConnection(); Stringsql = " select sysdate from dual"; PreparedStatement ps =con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { String value =rs.getString("sysdate"); System.out.println(StringUtils.center(value+",数据库连接成功", 50, "-")); } }catch(Exception e) { e.printStackTrace(); }finally{ if (con != null) { con.close(); } } } }

?

运行如下:

?

----------2014-12-1612:38:32.0,数据库连接成功-----------

5、运行结果

运行如下:

?

----------2014-12-1612:38:32.0,数据库连接成功-----------