tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Micael Padraig Og mac Grene" <>
Subject OKAY: HERE GOES. THIS IS GETTING INTERESTING. Re: Tomcat 4.0/JDBC driver configuration?
Date Thu, 29 Nov 2001 18:17:16 GMT
Hi, Mark,

Moving on, I see.  Great!  Here is a bunch of junk to look at and maybe
learn from.  (I don't mind ending sentences prepositions with.)  First is
this little way to get a connection from a PostgreSQL driver using
DataSource.  I assume you will be using DataSource and not the kiddies
version of jdbc, i.e. you will be using the jdbc extension, I assume.  If
not, you should.  I don't want to bother talking about the kiddy version,
which is well-covered in Sun tutorials.  If I seem to talk about the kiddy
version with disdain, that is the correct impression. ///;-)

Once you have the industrial strength jdbc set up with DataSource, the rest
should be somewhat obvious.  There is nothing really tricky or special about
Tomcat in this regard.

What I definitely like to do is to get all the database "parameters" as you
say established with start up in Tomcat, so I put it all in a StartUp
servlet that has an init() method.  Before I go on, I will stop and ask if
you know what an init() method with a servlet does?

NOTICE THAT THE jdbcURL, etc. used in the start up servlet come from the
web.xml specification that is for the start up servlet and ServletConfig,
i.e. are for init() parameters in web.xml.

Also note the resource reference in the web.xml at the bottom.  You are
beginning to get into a bit more complicated area.  You have to include your
jar or classes for a driver just like you do any classes or jars.  Jars are
jars and classes are classes and they all are found in the same way.  I also
use Tomcat in the context of an application server, running in the same JVM,
so I have not shown a lot I do in relation to JMX specifications and MBeans
and MLET classes which pop all I show here into a JNDI context.  But, that
should not be a problem.

Please do not fail to stop and learn the DataSource and jdbc extension
stuff.  You have to get an advanced driver when you do this.  The kiddy
drivers do not work with or have DataSource implementations.  You need to do
some scouting and reading on this, if you are not already up to speed.  When
I find out how much you know about this, then I can point you better.


Below are:

   1.  A little made up class to get a DataSource connection or Connection

    2.   A more developed web.xml than I have shown you before.

    3.  A StartUp servlet which is fairly sophisticated.

Have fun and let me know how it goes, if you would.  I have an interest in
seeing how you progress with all this, because we all have to train people
new at things.

Micael, again



public class PostgresqlConnection {
    private Connection conn;
    private String     user;
    private String     password;

    public Connection getConnection(String user, String password) throws
ClassNotFoundException, SQLException {
        PostgresqlDataSource dataSource = new PostgresqlDataSource();
  conn = dataSource.getConnection(user, password);
        return conn;


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"























     <description>Primary database</description>

       <web-resource-name>Credit Card Page</web-resource-name>



import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import beans.jdbc.DbConnectionPool;

public class SetupServlet extends HttpServlet implements, tags.jdbc.Constants {
   private DbConnectionPool pool;

   public void init(ServletConfig config) throws ServletException{

      ServletContext ctx = config.getServletContext();
      createDbConnectionPool(config, ctx);

      try {
         ctx.setAttribute(USERS_KEY, loadUsers(ctx));
      } catch(SQLException ex) {
         throw new ServletException(ex);

   public void destroy() {
      ServletContext ctx = getServletConfig().getServletContext();

      pool = null;

   private void createDbConnectionPool(ServletConfig config, ServletContext
ctx) {
      pool = new DbConnectionPool(
      ctx.setAttribute(DBPOOL_KEY, pool);

   private Users loadUsers(ServletContext ctx) throws SQLException {
      Connection conn = null;

      if(pool != null) {
         try {
            // wait for a maximum of 10 seconds for a connection
            // if pool is full
               conn = (Connection)pool.getConnection(10000);
         } catch(Exception ex) {
            throw new SQLException(ex.getMessage());
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * FROM USERS");
         Users users = new Users(rs);

         return users;
      return null;

-----Original Message-----
From: Mark <>
To: <>
Date: Thursday, November 29, 2001 8:58 AM
Subject: Tomcat 4.0/JDBC driver configuration?

>Can someone point me in the right direction or provide an example of
>configuring an application to use JDBC under Tomcat 4.0?  I was able to use
>both thin and OCI Oracle JDCB drover connections by copying the
> to $CATALINA_HOME/common/lib directory and renaming it to
>classes12.jar, but that's not a good long term solution.  I found having
>the driver in the CLASSPATH doesn't work.
>I suspect web.xml and/or server.xml is the place to configure the
>deployment details.  Any examples or references to examples would be
>greatly appreciated.
>To unsubscribe:   <>
>For additional commands: <>
>Troubles with the list: <>

To unsubscribe:   <>
For additional commands: <>
Troubles with the list: <>

View raw message