File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Datasource usage in standalone application" Watch "Datasource usage in standalone application" New topic

Datasource usage in standalone application

Prasanna Engineer

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.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

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.

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:
subject: Datasource usage in standalone application
It's not a secret anymore!