This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Datasource usage in standalone application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Datasource usage in standalone application" Watch "Datasource usage in standalone application" New topic
Author

Datasource usage in standalone application

Prasanna Engineer
Greenhorn

Joined: Dec 12, 2012
Posts: 1
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

Joined: Mar 07, 2010
Posts: 1646
    
    7

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.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Datasource usage in standalone application
 
Similar Threads
Share information between Web App and Stand-alone Java App
Problem with JBoss
JNDI lookup problem
Datasource/EJB
WSAD stand alone JNDI setting