• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Problems with DataSource and Tomcat

Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, when i try to execute the following code:
package trilcejf;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.*;
import javax.sql.DataSource;
public class EjemploDataSource{
public static void main(String args[]){
Connection con=null;

InitialContext initialContext=new InitialContext();

Context context=(Context)initialContext.lookup("java:comp/env");

DataSource ds=(DataSource) context.lookup("jdbc/ejemploDS");


//Obtenci�n de un objeto Statement para ejecutar sentencias SQL
Statement stmt=con.createStatement();

//Ejecuci�n de la sentencia SQL y obtenci�n de resultados en un objeto ResultSet
String sentenciaSQL="SELECT * from DatosAlumnos";
ResultSet rs=stmt.executeQuery(sentenciaSQL);

//Muestra de resultados mediante un bucle que recorre los registros que verifican la sentencia
String nombre,apellido,telefono;
System.out.println("Nombre :"+nombre+". Apellido :"+apellido+". Telefono: "+telefono);
}catch(NamingException e){
System.out.println("Error en el DataSource: "+e.toString());
}catch(SQLException e){
System.out.println("Excepcion capturada de SQL: "+e.toString());

//Cierre de la conexi�n
}catch(SQLException e){
System.out.println("No se puede cerrar la conexion: "+e.toString());
I get
Error en el DataSource: javax.naming.NoInitialContextException: Need to specify
class name in environment or system property, or as an applet parameter, or in a
n application resource file: java.naming.factory.initial
Exception in thread "main" java.lang.NullPointerException
at trilcejf.EjemploDataSource.main(EjemploDataSource.java:64)
I have defined in tomcat_home\webapps
JNDIDataSource.xml with the following:
<?xml version="1.0" encoding="iso-8859-1"?>
<Context path="/JNDIDataSource" docBase="JNDIDataSource" debug="1" reloadable="true"

<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_JNDIDataSource_log."
suffix=".txt" timestamp="true"/>

<Resource name="jdbc/ejemploDS" auth="Container" type="javax.sql.DataSource" scope="Shareable"
description="DBCP Connection Pool DataSource" />

<ResourceParams name="jdbc/ejemploDS">
And my application is under tomcat_home\webapps and its name is JNDIDataSource
Any suggestions about why the javax.naming.NoInitialContextException is thrown??
Thanks in advance
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple of things to look at. First, what version of Tomcat are you running? 4.1.2x is configured different than 5.0.x; if you are running 4.1.24 (or something similar) then you should be correctly configured. If you are running 5.0.9, try putting your JNDIDataSource.xml file in tomcat_home\conf\Catalina\localhost.
Second, make sure that the JDBC driver (for org.hsqldb.jdbcDriver) JAR files are in tomcat_home\common\lib. Usually JAR files for your application can be put in tomcat_home\webapps\JNDIDataSource\WEB-INF\lib, but for resources defined at this level the JAR files must be in the "global" library.
Hopefully one of these things will work for you.
Posts: 67504
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is much more about the Tomcat config than the JDBC code so I'm going to shuffle this along to the Tomcat forum.
    Bookmark Topic Watch Topic
  • New Topic