Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access websphere datasource using Java client

 
Venu Gopal Burra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try:
con=source.getConnection("username", "pwd");
 
Kurtcebe Eroglu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic