Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
Datasource Connection with Standalone Java Application

I have been able to configure my datasource in a web app but am unable to replicate that in a stand alone app.

Datasource is configured as server.xml file in tomcat server in config folder. 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);

Its gives 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 tried using web application is executed fine.

username, password are redacted since it is production environment.

Here is the jdbc.java :

public static void main(String[] args) throws Exception {

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:comp/env");
    DataSource ds = (DataSource) envContext.lookup("jdbc/haryana");
    //Connection conn = ds.getConnection();

    try {

        DatabaseMetaData dataSource = null;
        conn = ds.getConnection();
    } catch (Exception e) {
        System.out.println("Exception while getting local JDBC Connection getLogsJdbcConnection(): " + e.getMessage());
    }

Context.xml :

<Context crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/haryana" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="postgres"
password="postgres"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/fps_ws_db_feb17"/>
</Context>

web.xml file :

<resource-ref>
<description>Database connection resource</description>   
<res-ref-name>jdbc/haryana</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

can anyone please help me to rectify the this Exception.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!