Olá,
Estou com problemas para acessar um banco de dados mysql instalado no meu pc através de uma DataSource. Estou seguindo o exemplo de um livro de JSP e já consegui fazer os exemplos anteriores se conectarem com a BD, só que quando fui tentar utilizar a tal DataSource apareceu o seguinte erro do Tomcat no console:
Error occurred org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘’ for connect URL ‘null’
Acho que a minha jsp não está conseguindo encontrar o objeto DataSource através da JNDI. A jsp é a seguinte:
<%@page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<html>
<head>
<title>Using a DataSource</title>
</head>
<body>
<h1>Using a DataSource</h1>
<%
DataSource ds = null;
Connection conn = null;
ResultSet result = null;
Statement stmt = null;
ResultSetMetaData rsmd = null;
try{
Context context = new InitialContext();
Context envCtx = (Context) context.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/address");
if (ds != null) {
conn = ds.getConnection();
stmt = conn.createStatement();
result = stmt.executeQuery("SELECT * FROM AddressList");
}
}
catch (SQLException e) {
System.out.println("Error occurred " + e);
}
int columns=0;
try {
rsmd = result.getMetaData();
columns = rsmd.getColumnCount();
}
catch (SQLException e) {
System.out.println("Error occurred " + e);
}
%>
<table width="90%" border="1">
<tr>
<% // write out the header cells containing the column labels
try {
for (int i=1; i<=columns; i++) {
out.write("<th>" + rsmd.getColumnLabel(i) + "</th>");
}
%>
</tr>
<% // now write out one row for each entry in the database table
while (result.next()) {
out.write("<tr>");
for (int i=1; i<=columns; i++) {
out.write("<td>" + result.getString(i) + "</td>");
}
out.write("</tr>");
}
// close the connection, resultset, and the statement
result.close();
stmt.close();
conn.close();
} // end of the try block
catch (SQLException e) {
System.out.println("Error " + e);
}
// ensure everything is closed
finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {}
}
%>
</table>
</body>
</html>
O web.xml da minha aplicaçao está desse jeito:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
<res-ref-name>jdbc/address</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
e eu acrescentei o seguinte elemento no arquivo server.xml do Tomcat
logo antes do elemento </host> de fechamento próximo do final do arquivo
<!--O trexo abaixo configura o datasource para que os servlets possam se conectar com a base de dados-->
<Context path="/chapter14" docBase="chapter14" debug="0" reloadable="true">
<ResourceParams name="jdbc/address">
<parameter>
<name>password</name>
<value>1234</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/ADDRESS</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>admin</value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
O .jar do driver e os arquivos commons-dbcp.jar e commons-pool.jar já estão dentro da pasta de bibliotecas do tomcat. Sinceramente não entendo como essa estória de dataSource funciona, apesar de entender alguma coisa de JNDI.
Agradeço as respostas,
Fischer
[/code]
.