C3P0连接池

时间:2021-08-22 04:47:25
【文件属性】:
文件名称:C3P0连接池
文件大小:2.29MB
文件格式:ZIP
更新时间:2021-08-22 04:47:25
c3p0 package cn.tedu.pool; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Test { public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; Statement stat =null; ResultSet rs=null; ComboPooledDataSource cpds = new ComboPooledDataSource(); //ComboPooledDataSource cpds = new ComboPooledDataSource("config1"); try{ /*cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql:///mydb1"); cpds.setUser("root"); cpds.setPassword("");*/ conn=cpds.getConnection(); stat=conn.createStatement(); rs=stat.executeQuery("select * from account" ); while(rs.next()){ int id = rs.getInt(1); String name = rs.getString("name"); double money = rs.getDouble("money"); System.out.println(id+":"+name+":"+money); } }catch(Exception e){ e.printStackTrace(); }finally{ if(rs !=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ rs=null; } } if(stat !=null){ try { stat.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ stat =null; } } if(conn !=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ conn =null; } } } } }

网友评论