Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program to connect Oracle Database using DataSource interface

Tags:

java

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.

like image 489
Amrendu Pandey Avatar asked Jan 08 '14 10:01

Amrendu Pandey


People also ask

What creates a DataSource to connect to a database?

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.


1 Answers

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).

like image 142
Stephan Avatar answered Oct 14 '22 00:10

Stephan