Produce a `DataSource` object for Postgres JDBC, programmatically

Viewed 15903

The JDBC Tutorial recommends using a DataSource object to obtain database connections rather than using the DriverManager class. To quote the Connecting with DataSource Objects page:

DataSource objects … the preferred means of getting a connection to a data source.

How do I get such an object for a JDBC connection to Postgres? I have a JDBC driver in place.

Right now, I do not want to fiddle around with JNDI like this or this.

Can I instantiate a DataSource programmatically within my Java app? Or must I implement that DataSource interface myself?

1 Answers
Related