4

We want our own db connection configuration instead of using JNDI, but in the meantime, we also want to use DataSource instead of using DriverManager, how to do so?

imgen
  • 2,803
  • 7
  • 44
  • 64
  • have a look at [c3p0](http://www.mchange.com/projects/c3p0/) or [commons dbcp](http://sourceforge.net/projects/c3p0/) connection poolers – Arun P Johny Mar 22 '13 at 08:20

2 Answers2

7

You use a connection pool library like c3p0 or commons dbcp.

C3P0

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver            
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("dbuser");                                  
cpds.setPassword("dbpassword");

Connection connection = cpds.getConnection();

DBCP

BasicDataSource ds= new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl("jdbc:postgresql://localhost/testdb");
ds.setUsername("dbuser");
ds.setPassword("dbpassword");

Connection connection = ds.getConnection();
Greg Brown
  • 3,168
  • 1
  • 27
  • 37
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
3

You can use org.apache.commons.dbcp.BasicDataSource

BasicDataSource ds= new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@dburl:port:sid");
ds.setUsername("uname");
ds.setPassword("pass");
Anubhab
  • 1,736
  • 2
  • 18
  • 28