1 import java.sql.*; 2 import javax.naming.*; 3 import javax.sql.DataSource ; 4 import com.mchange.v2.c3p0.DataSources; 5 6 7 11 public final class JndiBindDataSource 12 { 13 static 17 { 18 try 19 { Class.forName( "org.postgresql.Driver" ); } 20 catch (Exception e) 21 { e.printStackTrace(); } 22 } 23 24 public static void main(String [] argv) 25 { 26 try 27 { 28 String jndiName = argv[0]; 31 32 DataSource unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/test", 35 "swaldman", 36 "test"); 37 DataSource pooled = DataSources.pooledDataSource( unpooled ); 38 39 InitialContext ctx = new InitialContext(); 46 ctx.rebind( jndiName, pooled ); 47 System.out.println("DataSource bound to nameservice under the name \"" + 48 jndiName + '\"'); 49 } 50 catch (Exception e) 51 { e.printStackTrace(); } 52 } 53 54 static void attemptClose(ResultSet o) 55 { 56 try 57 { if (o != null) o.close();} 58 catch (Exception e) 59 { e.printStackTrace();} 60 } 61 62 static void attemptClose(Statement o) 63 { 64 try 65 { if (o != null) o.close();} 66 catch (Exception e) 67 { e.printStackTrace();} 68 } 69 70 static void attemptClose(Connection o) 71 { 72 try 73 { if (o != null) o.close();} 74 catch (Exception e) 75 { e.printStackTrace();} 76 } 77 78 private JndiBindDataSource() 79 {} 80 } 81 | Popular Tags |