• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Help with Error!!

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it's just a test, I tried it in a servlet. It worked. Thanks for the explaination!
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic