Question

5
Replies
1249
Views
THARUKA89 Member since 2012 1 post
Virtusa Pvt Ltd
Posted: 4 years ago
Last activity: 4 years 10 months ago
Closed
Solved

How to Connect to an external DB using JNDI DataSource Object?

Hi,

I was trying to connect to an external DB using a DataSource Object with JNDI Naming service. I want to get the connection in side a java step.

Below is the code I'm trying,

HashStringMap keysMap = new HashStringMap();

keysMap.put("pyRWDatabase","DBName");

keysMap.put("pxObjClass","Data-Admin-DB-Name");

ClipboardPage dbPage = tools.getDatabase().open(keysMap,false);

javax.naming.Context ctx = new InitialContext();

javax.sql.DataSource ds = new DataSource();

ds = (DataSource)ctx.lookup(dbPage.getString("pyDataSourceJNDIName"));


java.sql.Connection Conn = ds.getConnection(dbPage.getString("pyDBUser"),dbPage.getString("pyDBPassword"));


String query = "select * from dual....";


java.sql.PreparedStatement cs= Conn.prepareStatement(query);

java.sql.ResultSet result = cs.executeQuery();

But the activity can't resolve Context and DataSource objects. Are there any predefined libraries to include or any other way to get the connection? 

Please help.

Note: Connection is fine with JDBC URL.

Data Integration
Moderation Team has archived post
Share this page LinkedIn