Ok for completeness...

This morning with a fresh start I managed to get the connection
working... this is my solution (hope it is of use to someone) thanks for
all the help and tips people gave me.

Environment :
Fedora Core 1
Tomcat 5.0.19
Apache 2.x
Java 1.4.2_03

Without using the admin tool in tomcat (as I'm not convinced about it) I
created the following, this is based on the example DBTest from
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html 
$CATALINA_HOME/webapps/DBTest/test.jsp

<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>
 
  <%
    foo.DBTest tst = new foo.DBTest();
    tst.init();
  %>
 
  <h2>Results</h2>
    Foo <%= tst.getFoo() %><br/>
    Bar <%= tst.getBar() %>
 
  </body>
</html>

$CATALINA_HOME/webapps/DBTest/WEB-INF/classes/foo/DBTest.java

package foo;
 
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
 
public class DBTest {

  String foo = "Not Connected";
  int bar = -1;

  public void init() {
    try{
      Context ctx = new InitialContext();
      if ( ctx == null ) {
        throw new Exception( "Boom - No Context" );
      }

      DataSource ds = ( DataSource )ctx.lookup(
"java:comp/env/jdbc/TestDB" );
 
      if (ds != null) {
        Connection conn = ds.getConnection();
 
        if(conn != null)  {
          foo = "Got Connection "+conn.toString();
                  Statement stmt = conn.createStatement();
                  ResultSet rst = stmt.executeQuery( "SELECT id, foo, bar  FROM
testdata" );
          if ( rst.next() ) {
            foo=rst.getString(2);
            bar=rst.getInt(3);
          }
          conn.close();
        }
      }
    } catch( Exception e ) {
      e.printStackTrace();
    }
  }
 
  public String getFoo() { return foo; }
  public int getBar() { return bar;}
}

$CATALINA_HOME/webapps/DBTest/WEB-INF/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/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

$CATALINA_HOME/conf/Catalina/localhost/DBTest.xml

<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true"
crossContext="true">
  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_DBTest_log." suffix=".txt"
             timestamp="true"/>
  
  <Resource name="jdbc/TestDB"
               auth="Container"
               type="javax.sql.DataSource"/>
  
  <ResourceParams name="jdbc/TestDB">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
  
    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>100</value>
    </parameter>
  
    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>30</value>
    </parameter>
  
    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>
  
    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>javauser</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>javadude</value>
    </parameter>
  
    <!-- Class name for com.mysql JDBC driver -->
    <parameter>
       <name>driverClassName</name>
       <value>com.mysql.jdbc.Driver</value>
    </parameter>
  
    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld
closed the
         connection.  mysqld by default closes idle connections after 8
hours.
         -->
    <parameter>
      <name>url</name>
     
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
    </parameter>
  </ResourceParams>
  
</Context>

I compiled DBTest.java started up tomcat and went to
localhost:8080/DBTest/test.jsp initially I had an error but that was due
to the privileges being wrong for the MySQL user (the connection was
using the server domain name rather than localhost) once that was sorted
out (MySQL privileges change in this case) the example then worked.

Anyway, as I said at the top thanks to anyone that passed on some tips.

;-)

Off to do the rest of the code I need for this app now

Regards
Dave

-- 
David Smith <[EMAIL PROTECTED]>

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

Reply via email to