File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Websphere and the fly likes How to access websphere datasource using Java client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Products » Websphere
Bookmark "How to access websphere datasource using Java client" Watch "How to access websphere datasource using Java client" New topic

How to access websphere datasource using Java client

Venu Gopal Burra

Joined: Nov 24, 2003
Posts: 1
Is there any way to access websphere data source using Normal Java Client.
I tried to access in this way
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class DSTest
public static void main(String args[])
Connection con=null;
java.util.Properties p = new java.util.Properties();

p.put(Context.PROVIDER_URL,"iiop://server ortno");
javax.naming.Context ctx = new javax.naming.InitialContext(p);

System.out.println("trying to lookup rpool jdbc datasource");
Object obj=ctx.lookup("comp:java/env/jdbc/datasourcename");
//And in this way also
//Object obj=ctx.lookup("jdbc/datasourcename");
System.out.println("This is object type-->"+obj.getClass());
//DataSource source = (javax.sql.DataSource) javax.rmi.PortableRemoteObject.narrow(obj, javax.sql.DataSource.class);
DataSource source = ( DataSource )obj;

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp ");
System.out.println("This is value"+rs.getString(1));
catch(Exception e)
System.out.println("Exception "+e);
catch(Exception e){}

When I tried to execute this program either I'm getting Name not found exception or class cast exception (i.e. at that time websphere is returning javax.naming.Reference then I'm trying to cast to Datasource)
Anybody can help me in this.
Thanks in advance.
Shubin Wei

Joined: Apr 22, 2011
Posts: 1
you can try:
con=source.getConnection("username", "pwd");
Kurtcebe Eroglu
Ranch Hand

Joined: Jan 30, 2007
Posts: 30
You must use Application Client for WebSphere Application Server to do that. You may use this page as an entry point to details of installing and using that.

Also, this page tells in details how to access your datasource afterwards. But be warned about the note below as quoted.

Note that data access from an application client uses the JDBC driver connection functionality directly from the client side. It does not take advantage of the additional pooling support available in the application server run time. For this reason, your client application should utilize an enterprise bean running on the server side to perform data access. This enterprise bean can then take advantage of the connection reuse and additional added functionality provided by the product run time.

I agree. Here's the link:
subject: How to access websphere datasource using Java client
It's not a secret anymore!