permaculture playing cards
The moose likes Tomcat and the fly likes Problems with DataSource and Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Problems with DataSource and Tomcat" Watch "Problems with DataSource and Tomcat" New topic

Problems with DataSource and Tomcat

Jesus Fernandez

Joined: Aug 11, 2003
Posts: 14
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(
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
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63355

This is much more about the Tomcat config than the JDBC code so I'm going to shuffle this along to the Tomcat forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Problems with DataSource and Tomcat
It's not a secret anymore!