Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help with Error!!

 
Elle Atechsy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



I'm very new to Java and WSAD 5.1. I have a database connection method that I can call from any other class "named method" with NO a problem. However, whenever I try to call the same database connection method from a "main" method within other classes, I get the following error.

********ERROR Starts********

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:255)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:292)
at javax.naming.InitialContext.lookup(InitialContext.java:359)
at com.ubs.mcs.oos.dao.RDBMSBaseDAO.getDatabaseConnection(RDBMSBaseDAO.java:51)
at com.ubs.mcs.oos.testClasses.testClassCall.main(testClassCall.java:30)
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initialSomething is wrong!!!

at com.ubs.mcs.oos.dao.RDBMSBaseDAO.getDatabaseConnection(RDBMSBaseDAO.java:60)
at com.ubs.mcs.oos.testClasses.testClassCall.main(testClassCall.java:30)

********ERROR ENDS********

Here is what my database connection method looks like:

********DB Connection Method Starts********
public synchronized Connection getDatabaseConnection(String appName)
throws DAOException {

String pool = appName + "Pool";
try {
javax.sql.DataSource ds = (javax.sql.DataSource) getInitialContext().lookup(pool);
Connection conn = ds.getConnection();
return conn;
} catch (Exception e){
e.printStackTrace();
throw new DAOException(e);
}
}
********DB Connection Method ENDS********

********Call to DB Connection Method from w/i Main method of another class.

conn = getElems.getDatabaseConnection(mesRes.getMessage("appID.acronym"));



I appreciate any help provided.

Thanks,
Lulu
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lulu,
When you run code from a main method, you are outside the server's JVM. This code would work if run from an EJB or servlet.

If you are running from an application client, WSAD will recognize the initial context. If not, you have to pass remote parameters. Search this, the J2EE and WebSphere forums for some examples.
 
Elle Atechsy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's just a test, I tried it in a servlet. It worked. Thanks for the explaination!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic