Folks, 

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. 

java.lang.NullPointerException 
org.apache.jsp.usingDataSource_jsp._jspService(usingDataSource_jsp.java:74) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:856) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:856) 

Here is the JSP page I'm using: 

<[EMAIL PROTECTED] 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(); // bad line
    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>


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"> 
    <parameter> 
      <name>username</name> 
      <value>username</value> 
    </parameter> 
    <parameter> 
      <name>password</name> 
      <value>password</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> 
  </ResourceParams> 
</Context> 

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" 
    "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> 

Rob





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to