Solved

Tomcat and JNDI Configuration

Posted on 2004-10-11
4
256 Views
Last Modified: 2010-04-20
I configured my MySQL database to have the following:

<Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="false" debug="0" docBase="c:\Tomcat\webapps\SOG" mapperClass="org.apache.catalina.core.StandardContextMapper" path="/SOG" privileged="false" reloadable="true" swallowOutput="false" useNaming="false" wrapperClass="org.apache.catalina.core.StandardWrapper">
          <Resource name="jdbc/SOGDB" auth="Container" scope="Shareable" type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/SOGDB">
            <parameter>
              <name>url</name>
              <value>jdbc:mysql://localhost:3306/sog</value>
            </parameter>
            <parameter>
              <name>password</name>
              <value>webuser</value>
            </parameter>
            <parameter>
              <name>maxActive</name>
              <value>40</value>
            </parameter>
            <parameter>
              <name>maxWait</name>
              <value>5000</value>
            </parameter>
            <parameter>
              <name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value>
            </parameter>
            <parameter>
              <name>user</name>
              <value>soguser</value>
            </parameter>
            <parameter>
              <name>maxIdle</name>
              <value>40</value>
            </parameter>
          </ResourceParams>
        </Context>

Here is my web.xml:
<?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>
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/SOGDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>


Then I try it with the following code:
                  Context ctx = new InitialContext();
                  if(ctx == null ) throw new Exception("Boom - No Context");
                        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/SOGDB");
                  Connection conn = ds.getConnection();
                  Statement stmt = conn.createStatement();
                  ResultSet rs = stmt.executeQuery("select * from membership");
                  while (rs.next())
                  {
                        out.println(rs.getString(2));
                  }

I get an error:

Name java:comp not bound in this context.  I've successfully set up Microsoft SQL Server 2000 drivers in JNDI using the same methodology.  
0
Comment
Question by:techhound
  • 3
4 Comments
 
LVL 13

Expert Comment

by:petmagdy
Comment Utility
Did u included the my SQL JDBC Driver in the Tomcat classpath?
0
 
LVL 13

Expert Comment

by:petmagdy
Comment Utility
Also please print out the Error stack trace by

err..printStackTrace();
0
 

Author Comment

by:techhound
Comment Utility
The JDBC driver for MySQL is in the %TOMCAT_HOME%\common\lib directory.  I ran the above code using a jsp page.  Therefore, wouldn't it find the appropriate drivers from the directory?  The stack trace that I get is in the browser itself and only points to the compiled jsp page catch area.  I can still print the stack trace if you think that will help but where would I put then in the jsp page?  Should I set up a try/catch there and print it in the catch?

Thanks
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 500 total points
Comment Utility
The MySQL driver lock like this (mm.mysql-2.0.7-bin.jar) right?

in your jsp do this to get the print stack trace:

<%

try
{
 //your code
}
catch(Exception err)
{
  err.printStackTrace();
}

%>

this will display the error on the tomcat console or tomcat log file
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now