Discussion

How to retrieve an InitialContext (JNDI) with Java

Hi folks, I try to get a database connection from within Java code using the DataSource of an Jboss Application server running PRPC. I used the following code. However it stuck with a NamingException in the InitialContext creation. What is wrong here? ... java.sql.Connection conn = null; javax.naming.InitialContext ctx = null; javax.sql.DataSource ds = null; try { ctx = new javax.naming.InitialContext(); if (ctx == null) { debugg += "ERROR: InitialContext not retrievable.\n"; tools.getProperty("CSVPage.Testprop").setValue(debugg); return false; } } catch (javax.naming.NamingException ne) { debugg += "ERROR: InitialContext not retrievable. NamingException\n"; tools.getProperty("CSVPage.Testprop").setValue(debugg); return false; } try { ds = javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES"); if (ds == null) { debugg += "DataSource lookup failure.\n"; tools.getProperty("CSVPage.Testprop").setValue(debugg); return false; } } catch (javax.naming.NamingException ne2) { debugg += "ERROR: InitialContext not retrievable. NamingException\n"; tools.getProperty("CSVPage.Testprop").setValue(debugg); return false; } try { conn = ds.getConnection(); debugg = debugg + "Connection established with (" +url+", "+user+", "+password+")"; ... ... Thanks for answers!

***Updated by moderator: Marissa to close post***
This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Comments

Keep up to date on this post and subscribe to comments

March 12, 2009 - 10:50am

I need to do something like this as well. I'm trying to create a Java step that calls an Oracle stored procedure which returns a ref cursor. Java step is the best option for me but I don't even know how to get to the DB from within the PRPC Java step.

quote:


Originally posted by TRIPATHA

Hi folks,

I try to get a database connection from within Java code using the
DataSource of an Jboss Application server running PRPC.

I used the following code. However it stuck with a NamingException
in the InitialContext creation. What is wrong here?

...
java.sql.Connection conn = null;
javax.naming.InitialContext ctx = null;
javax.sql.DataSource ds = null;
try {
ctx = new javax.naming.InitialContext();
if (ctx == null) {
debugg += "ERROR: InitialContext not retrievable.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
try {
ds = javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES");
if (ds == null) {
debugg += "DataSource lookup failure.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne2) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}

try {
conn = ds.getConnection();
debugg = debugg + "Connection established with (" +url+", "+user+", "+password+")";
...

...

Thanks for answers!


March 12, 2009 - 3:19pm

Just added this so I could subscribe to the topic

quote:


Originally posted by STERLINJ

I need to do something like this as well. I'm trying to create a Java step that calls an Oracle stored procedure which returns a ref cursor. Java step is the best option for me but I don't even know how to get to the DB from within the PRPC Java step.

quote:


Originally posted by TRIPATHA

Hi folks,

I try to get a database connection from within Java code using the
DataSource of an Jboss Application server running PRPC.

I used the following code. However it stuck with a NamingException
in the InitialContext creation. What is wrong here?

...
java.sql.Connection conn = null;
javax.naming.InitialContext ctx = null;
javax.sql.DataSource ds = null;
try {
ctx = new javax.naming.InitialContext();
if (ctx == null) {
debugg += "ERROR: InitialContext not retrievable.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
try {
ds = javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES");
if (ds == null) {
debugg += "DataSource lookup failure.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne2) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}

try {
conn = ds.getConnection();
debugg = debugg + "Connection established with (" +url+", "+user+", "+password+")";
...

...

Thanks for answers!



March 13, 2009 - 2:46am

---------------
Replace the following code to following highlited lines

ds = (javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES");
if (ds == null)

try {
here "(" is missing in the following line
ds = javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES");
if (ds == null)
{
debugg += "DataSource lookup failure.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne2) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}

now it will be fine I think.

Cheers.,
Ajay

----------------

quote:


Originally posted by TRIPATHA

Hi folks,

I try to get a database connection from within Java code using the
DataSource of an Jboss Application server running PRPC.

I used the following code. However it stuck with a NamingException
in the InitialContext creation. What is wrong here?

...
java.sql.Connection conn = null;
javax.naming.InitialContext ctx = null;
javax.sql.DataSource ds = null;
try {
ctx = new javax.naming.InitialContext();
if (ctx == null) {
debugg += "ERROR: InitialContext not retrievable.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
} catch (javax.naming.NamingException ne) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}
try {
ds = javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/pegaRULES");
if (ds == null) {
debugg += "DataSource lookup failure.\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}[/b]} catch (javax.naming.NamingException ne2) {
debugg += "ERROR: InitialContext not retrievable. NamingException\n";
tools.getProperty("CSVPage.Testprop").setValue(debugg);
return false;
}

try {
conn = ds.getConnection();
debugg = debugg + "Connection established with (" +url+", "+user+", "+password+")";
...

...

Thanks for answers!


March 13, 2009 - 3:18am

Hi

Sorry,
I didn't get what are you doing?
I think we can use the PublicAPI to get the database details to run the queries in a Java step.
some sample code is as follows:

ClipboardPage dbPage = tools.createPage("Code-Pega-List", "");
dbPage.putString("pyObjClass", "Give the class name on which you want to execute the query");
dbPage.putString("pyListName", "List");
dbPage.putString("pyMaxRecords", "1000");

dbPage.putString(".pyCondition(1).pyConditionFieldName", "Give the property name which will be used in the where clause of the query");
dbPage.putString(".pyCondition(1).pyConditionOperation", "=");
dbPage.putString(".pyCondition(1).pyConditionValue1","RHS of the where clause");

int i;
try { i = tools.getDatabase().list(dbPage, false); }
catch (DatabaseException ex)
{
oLog.error(ex.getMessage());
}

ClipboardProperty results=dbPage.getProperty("pxResults");

now the results is the pxResults pagelist of the executed query.
create a iterator for this page and iterate through the pxResults.

cheers.

March 13, 2009 - 4:43pm

There is no query. I'm calling an Oracle stored procedure that takes parameters and returns a ref cursor. I "hijacked" this topic because I was stuck even at the beginning of how to get a reference to the external database (the stored procedure is not in the PRPC database pegaRULES). We have other code in PRPC that hits the same database but is only calling a stored procedure that returns single values instead of a ref cursor. I mention this because that means there is already a reference to this external database - the IDM database - that I need to get to from my Java step. I'll try that context look up again.

In summary, this is what I'm trying to do:
a) get a connection to the database (though JNDI or maybe I can use something in the tools object that is mentioned).

b) create a callable statement and set parameters
c) convert the returned cursor to a result set. Something like this:
connection = getConnection(connection);
cst = getCallableStmt(connection, cst, procedureStmt);
if(cst!=null){
cst.setString(1,dealId);
cst.registerOutParameter(2, OracleTypes.CURSOR);

cst.execute();
rs = (ResultSet)cst.getObject(2);

if (rs != null) {
String fullName = "";
while (rs.next()) {
com.jpmorganchase.idm.model.User user = new com.jpmorganchase.idm.model.User();

user.setStandardId(rs.getString("standardId"));

quote:


Originally posted by shivakumars

Hi

Sorry,
I didn't get what are you doing?
I think we can use the PublicAPI to get the database details to run the queries in a Java step.
some sample code is as follows:

ClipboardPage dbPage = tools.createPage("Code-Pega-List", "");
dbPage.putString("pyObjClass", "Give the class name on which you want to execute the query");
dbPage.putString("pyListName", "List");
dbPage.putString("pyMaxRecords", "1000");

dbPage.putString(".pyCondition(1).pyConditionFieldName", "Give the property name which will be used in the where clause of the query");
dbPage.putString(".pyCondition(1).pyConditionOperation", "=");
dbPage.putString(".pyCondition(1).pyConditionValue1","RHS of the where clause");

int i;
try { i = tools.getDatabase().list(dbPage, false); }
catch (DatabaseException ex)
{
oLog.error(ex.getMessage());
}

ClipboardProperty results=dbPage.getProperty("pxResults");

now the results is the pxResults pagelist of the executed query.
create a iterator for this page and iterate through the pxResults.

cheers.