Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

How to access websphere datasource using Java client

Venu Gopal Burra
Greenhorn

Joined: Nov 24, 2003
Posts: 1
Hi,
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;
try
{
java.util.Properties p = new java.util.Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

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;

con=source.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp ");
while(rs.next())
{
System.out.println("This is value"+rs.getString(1));
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
e.printStackTrace();
}
finally
{
try
{
con.close();
}
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
Greenhorn

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: http://aspose.com/file-tools
 
subject: How to access websphere datasource using Java client
 
Similar Threads
Sybase JNDI lookup woes
JNDI Lookup help in websphere 4.0
error while running Client in Eclipse helios
creating a WAS client.
error while running session bean