?
Solved

Tomcat and JNDI Configuration

Posted on 2004-10-11
4
Medium Priority
?
263 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:James Cochrane
  • 3
4 Comments
 
LVL 13

Expert Comment

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

Expert Comment

by:petmagdy
ID: 12306457
Also please print out the Error stack trace by

err..printStackTrace();
0
 
LVL 1

Author Comment

by:James Cochrane
ID: 12306592
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 2000 total points
ID: 12306830
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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