aspose file tools*
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
Author

Problems with DataSource and Tomcat

Jesus Fernandez
Greenhorn

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;
try{

InitialContext initialContext=new InitialContext();

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

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

con=ds.getConnection();

//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;
while(rs.next()){
nombre=rs.getString("nombre");
apellido=rs.getString("apellidos");
telefono=rs.getString("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
}finally{
try{
con.close();
}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"
crossContext="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">
<parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
<parameter><name>driverClassName</name><value>org.hsqldb.jdbcDriver</value></parameter>
<parameter><name>username</name><value>trilcejf</value></parameter>
<parameter><name>password</name><value>profe</value></parameter>
<parameter><name>url</name><value>jdbc:hsqldb:http://localhost/Alumnos</value></parameter>
<parameter><name>maxActive</name><value>10</value></parameter>
<parameter><name>maxIdle</name><value>30000</value></parameter>
<parameter><name>maxWait</name><value>100</value></parameter>
<parameter><name>removeAbandoned</name><value>true</value></parameter>
<parameter><name>removeAbandonedTimeout</name><value>60</value></parameter>
<parameter><name>logAbandoned</name><value>true</value></parameter>
</ResourceParams>
</Context>
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
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with DataSource and Tomcat