Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda