Solved

Tomcat and JNDI Configuration

Posted on 2004-10-11
4
258 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
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
 

Author Comment

by:techhound
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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Index.php in Codeigniter 12 71
Change local server setting in php 6 104
System Analysis 5 64
Link failure 16 60
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…

685 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