JNDI + tomcat

Olá Pessoal!

Estou tentando configurar a conexão com o banco de dados pelo tutorial
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html, porém obtenho o erro:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

Valeu!

Você está usando Tomcat 4.x?

Estou usando a versão 5.x, porém ja li um arquivo e mudei o seguinte no server.xml:

Antes:

<ResourceParams name="jdbc/TestDB">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>100</value>
    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>30</value>
    </parameter>

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>

    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>javauser</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>javadude</value>
    </parameter>

    <!-- Class name for mm.mysql JDBC driver -->
    <parameter>
       <name>driverClassName</name>
       <value>org.gjt.mm.mysql.Driver</value>
    </parameter>

    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
    <parameter>
      <name>url</name>
      <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
    </parameter>
  </ResourceParams>

Depois:

<Resource name="jdbc/TestDB" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest? autoReconnect=true"/>

Não é recomendavel criar isso no server.xml e sim dentro do seu contexto.

Coloquei apenas no contexto e não funcionou…

Você deve ter colocado algo errado, você pode criar um arquivo context.xml na pasta META-INF da sua aplicação, ou criar um arquivo NOME_DO_CONTEXTO.xml na pasta catalina_home/conf/Catalina/localhost

Coloquei dentro do META-INF da minha aplicação

<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
	
   <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
    -->

</Context>

E meu web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>DBTest</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>	
</web-app>

Existe algo errado???

Tente colocar o arquivo assim:

<Context> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" /> </Context>

Caso você não consiga fazer o lookup de jdbc/TestDB tente java:comp/env/jdbc/TestDB (não mude no context.xml, só no código)

Há algo errado na minha classe lookup???

package foo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBTest {
	String foo = "Not Connected";
	int bar = -1;

	public void init() {
		try {
			InitialContext initCtx = new InitialContext();
			if (initCtx == null)
				throw new Exception("Boom - No Context");
			DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/TestDB");			

			if (ds != null) {
				Connection conn = ds.getConnection();

				if (conn != null) {
					foo = "Got Connection " + conn.toString();
					Statement stmt = conn.createStatement();
					ResultSet rst = stmt
							.executeQuery("select id, foo, bar from testdata");
					if (rst.next()) {
						foo = rst.getString(2);
						bar = rst.getInt(3);
					}
					conn.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getFoo() {
		return foo;
	}

	public int getBar() {
		return bar;
	}

}

Parece que não, qual o erro?

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 

Problema resolvido! Obrigada a todos!

E vc pode postar a solução?