Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda