• 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

Datasource usage in standalone application

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Datasource is configured in server.xml file. How can I load the datasource from this file in a stand alone java application?
If i try with
Context initialContext = new InitialContext();
DataSource dataSource = (DataSource) csDBEnvContext.lookup(DATASOURCE_NAME);

i get the following exception
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

Same code when called from jsp gets executed fine. To my understanding, in this case container plays the key role in proper execution.

Datasource name alone is shared to me. username, password are not shared since it is production environment.

Thanks
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Java Ranch!

You should never be executing code like that from a JSP, I know it will work but it is very bad code smell. Data access stuff does not belong in the view layer.

You say standalone application. To be this means you are not running inside of a container. If this is the case you are not going to be able to look up the datasource using JNDI. Rather you are going to have to establish your own connection.

http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html


I am moving this to the JDBC forum as it does not look like a Spring related question.
 
reply
    Bookmark Topic Watch Topic
  • New Topic