• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with DataSource and Tomcat

 
Greenhorn
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;
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
 
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • 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.
bear
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic