i`ve tried to solve this problem, and i think i did it!

First of all:

I've tried with:
Tomcat 4.0.4
Mysql 3.23.49


You should have in your application's lib these libraries:
- commons-collections.jar
- commons-dbcp.jar
- commons-pool.jar
(You can find these jar in www.apache.org)
- mysql-uncomp.jar(driver package)

My server.xml looks like:

...
        <!--
################################################################ -->
        <DefaultContext>
                <Resource
                        name="jdbc/COLLECTIONS"
                        auth="Container"
                        type="javax.sql.DataSource"
                        description="BW Database"/>
                <ResourceParams
                        name="jdbc/COLLECTIONS">
                        <parameter>
                                <name>factory</name>
                                
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                        </parameter>
                        <parameter>
                                <name>driverClassName</name>
                                <value>org.gjt.mm.mysql.Driver</value>
                        </parameter>
                        <parameter>
                                <name>username</name>
                                <value>XXXXXXX</value>
                        </parameter>
                        <parameter>
                                <name>password</name>
                                <value>XXXXXXX</value>
                        </parameter>
                        <parameter>
                                <name>url</name>
                                <value>jdbc:mysql://localhost:3306/collections</value>
                        </parameter>
                </ResourceParams>
        </DefaultContext>
        <!--
################################################################ -->
...

and the class where i retrive a connection:


package tests;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

public class MysqlTomcatDatasourceTest extends HttpServlet {
        static final private String CONTENT_TYPE = "text/html";

        public void doGet(HttpServletRequest request,
                                        HttpServletResponse response)
                throws ServletException, IOException {

                Context t_ctx = null;
                DataSource t_ds = null;
                Connection t_conn = null;
                PreparedStatement t_stm = null;
                ResultSet t_rs = null;
                PrintWriter t_out = null;

                response.setContentType(CONTENT_TYPE);
                try {
                        t_out = response.getWriter();
                        t_out.println("<html>");
                        t_out.println("<head><title>Datasource</title></head>");
                        t_out.println("<body>");
                        t_out.println("<p>i'm in.</p>");
                        t_out.println("<p>");
                        t_ctx = new InitialContext();
                        t_ds = 
(DataSource)t_ctx.lookup("java:comp/env/jdbc/COLLECTIONS");
                        System.out.println("done");
                        t_conn = t_ds.getConnection();
                        t_stm = t_conn.prepareStatement("select countryname from 
countries");
                        t_rs = t_stm.executeQuery();
                        while (t_rs.next()){
                                t_out.println(t_rs.getString(1)+"<br>");
                        }
                        t_out.println("</p>");
                        t_out.println("</body></html>");
                        try {t_rs.close();}catch(SQLException e){}
                        try {t_stm.close();}catch(SQLException e){}
                        try {t_conn.close();}catch(SQLException e){}
                }catch (Exception e) {
                        throw new ServletException(e);
                }
        }
}

I hope this help



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <mysql-unsubscribe-##L=##[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to