I want to connect to Oracle database using DataSource interface not using DriverManager in java. I don't have an idea about this. Please provide me a sample program to do so.
A basic DataSource implementation produces standard Connection objects that are not pooled or used in a distributed transaction. A DataSource implementation that supports connection pooling produces Connection objects that participate in connection pooling, that is, connections that can be recycled.
When you want to use a DataSource here is the way to go:
// Setup the datasource
DataSource ds = new OracleDataSource();// There is other DataSource offered by Oracle , check the javadoc for more information
ds.setDriverType("thin");
ds.setServerName("myServer");
ds.setPortNumber(1521);
ds.setDatabaseName("myDB");
ds.setUser("SCOTT");
ds.setPassword("TIGER");
// Get a JDBC connection
Connection c = ds.getConnection();
This what is roughtly done under the cover.
However, in real life project, you won't often do this. Let's say you build a web application. Usually, you'll configure a datasource in text format and drop this configuration on your container. Later, you can retreive the datasource through JNDI (see @Radhamani Muthusamy answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With