This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Datasource usage in standalone application

 
Prasanna Engineer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic