You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Manolo Ramirez T." <ma...@handsoftware.net> on 2003/08/23 04:55:15 UTC

Can not load JNDI DataSource in Servlet.init()

Hi all,

Why I can't load a JNDI resource on the init method of my servlet? there 
is no problem doing that in doGet() but on init() it doesn't work. the 
logs entry is:

java.sql.SQLException: Cannot load JDBC driver class 'null'

It's the same code! What I'm missing?

this is my servlet code:






package libreria;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;


public class MyServlet extends HttpServlet {

     public void init (ServletConfig config) throws ServletException{
	super.init(config);
	try {
	    Context ctx = new InitialContext();
	    if(ctx==null) {
		System.out.println("fallo InitialContext");
		return;
	    }

	    DataSource ds = 
(DataSource)ctx.lookup("java:comp/env/jdbc/ComercialDB");
	    if(ds==null) {
		System.out.println("fallo lookup");
		return;
	    }

	    Connection conn = ds.getConnection();

	    Statement stm = conn.createStatement();
	
	    ResultSet rs = stm.executeQuery("select * from pruebas");

	    while(rs.next()) {
	 
System.out.println(rs.getInt("id")+"::"+rs.getString("nombre")+"::"+rs.getString("apellido"));
	    }
	}
	catch(Exception e) {
	    e.printStackTrace();
	}
     }

     public void doGet(HttpServletRequest request, HttpServletResponse 
response) throws IOException,ServletException {
	response.setContentType("text/plain");
	PrintWriter out= response.getWriter();
	out.println("holas muchas");

	try {
	    Context ctx = new InitialContext();
	    if(ctx==null) {
		out.println("fallo InitialContext");
		return;
	    }

	    DataSource ds = 
(DataSource)ctx.lookup("java:comp/env/jdbc/ComercialDB");
	    if(ds==null) {
		out.println("fallo lookup");
		return;
	    }

	    Connection conn = ds.getConnection();

	    Statement stm = conn.createStatement();
	
	    ResultSet rs = stm.executeQuery("select * from pruebas");

	    while(rs.next()) {
	 
out.println(rs.getInt("id")+"::"+rs.getString("nombre")+rs.getString("apellido"));
	    }
	}
	catch(Exception e) {
	    e.printStackTrace(out);
	}
     }
}





Regards.

_____________
Manolo Ramirez T.