1 import java.sql.*; 2 import javax.sql.DataSource ; 3 import com.mchange.v2.c3p0.DataSources; 4 5 6 10 public final class UsePoolBackedDataSource 11 { 12 13 public static void main(String [] argv) 14 { 15 try 16 { 17 20 DataSource unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/test", 22 "swaldman", 23 "test"); 24 DataSource pooled = DataSources.pooledDataSource( unpooled ); 25 26 27 28 Connection con = null; 30 Statement stmt = null; 31 ResultSet rs = null; 32 try 33 { 34 con = pooled.getConnection(); 35 stmt = con.createStatement(); 36 rs = stmt.executeQuery("SELECT * FROM foo"); 37 while (rs.next()) 38 System.out.println( rs.getString(1) ); 39 } 40 finally 41 { 42 attemptClose(rs); 49 attemptClose(stmt); 50 attemptClose(con); 51 } 52 } 53 catch (Exception e) 54 { e.printStackTrace(); } 55 } 56 57 static void attemptClose(ResultSet o) 58 { 59 try 60 { if (o != null) o.close();} 61 catch (Exception e) 62 { e.printStackTrace();} 63 } 64 65 static void attemptClose(Statement o) 66 { 67 try 68 { if (o != null) o.close();} 69 catch (Exception e) 70 { e.printStackTrace();} 71 } 72 73 static void attemptClose(Connection o) 74 { 75 try 76 { if (o != null) o.close();} 77 catch (Exception e) 78 { e.printStackTrace();} 79 } 80 81 private UsePoolBackedDataSource() 82 {} 83 } 84 | Popular Tags |