tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert S. Jones" <>
Subject Problem connecting to mysql database with Tomcat and JNDI
Date Thu, 21 Aug 2003 22:34:39 GMT

I'm having trouble connecting to a mysql database through Tomcat.  I'm
using Tomcat 5.0.9a with mysql 4.0.14.  I have the mysql 3.0.8 JDBC
driver in ${TOMCAT_HOME}/common/lib.  I'm running RedHat 8.0 with a
stock kernel. 

I've been able to connect to the database without JNDI services.  I
believe I have the database user set up correctly.  I have been all over
the web trying to figure out what the problem is.  It seems like
everyone is having this problem, but no two solutions are the same.

FWIW I've tried a lot of this on Tomcat 4 and have had the same results.

I've traced the problem to the following code from the JSP file below:

conn = ds.getConnection();

Any thoughts/ideas would be greatly appreciated.  I'm at the end of my
rope on this.

Here is the error message I'm getting. 


Here is the JSP page I'm using: 

<%@page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<title>Using a DataSource</title>
<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(); // bad line
    columns = rsmd.getColumnCount();
catch (SQLException e) {
    System.out.println("Error occurred " + e);
<table width="90%" border="1">
<% // write out the header cells containing the column labels
    try {
        for (int i = 1; i <= columns; i++) {
            out.write("<th>" + rsmd.getColumnLabel(i) + "</th>");
<% // now write out one row for each entry in the database table
        while ( {
            for (int i = 1; i <= columns; i++) {
                out.write("<td>" + result.getString(i) + "</td>");

        // close the connection, resultset, and the statement
    } // end of the try block
catch (SQLException e) {
    System.out.println("Error " + e);
// ensure everything is closed
finally {
    try {
        if (stmt != null) {
    } catch (SQLException e) {}
    try {
        if (conn != null) {
    } catch (SQLException e ) {}


Here are the changes I made to my server.xml file.  Of course, I've
changed the username/password entries here. 

<Context path="/db-test" docBase="db-test" debug="0" reloadable="true"> 
  <ResourceParams name="jdbc/address"> 

Here is the web.xml file I am using in my WEB-INF directory. 

<?xml version="1.0" encoding="ISO-8859-1"?> 
    <!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 


View raw message