In order to obtain a Datasource, which is already created in the websphere application server; you have to first update the web.xml entry of your web application with the below lines:

<resource-env-ref id="ResourceEnvRef_1309162527015">

<description>ref data source alias</description>





Once you update the web.xml entry use the below java code to obtain the data source:

public class GetConnection {

public static void main(String args[]){

// getJNDIConnection();

// getConnection();



/** Uses JNDI and Datasource (preferred style). */

static Connection getJNDIConnection(){

String DATASOURCE_CONTEXT = "refDataSource";

Connection result = null;

try {

Context initialContext = new InitialContext();

if ( initialContext == null){

System.out.println("JNDI problem. Cannot get InitialContext.");


DataSource datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);

if (datasource != null) {

result = datasource.getConnection();


else {

System.out.println("Failed to lookup datasource.");



catch ( NamingException ex ) {

System.out.println("Cannot get connection: " + ex);


catch(SQLException ex){

System.out.println("Cannot get connection: " + ex);


return result;



Enter your email address:

Delivered by FeedBurner

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on ; I would like to hear from you :)