commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Clark D. Richey, Jr." <cric...@speakeasy.net>
Subject RE: no suitable driver when using DBCP
Date Wed, 02 Jul 2003 02:07:35 GMT
That did it. Thanks!

-----Original Message-----
From: Graeme Rimmer [mailto:graeme.rimmer@lineone.net] 
Sent: Friday, June 27, 2003 5:27 AM
To: Jakarta Commons Users List
Subject: Re: no suitable driver when using DBCP

I had a similar problem, not sure I got the solution completely right 
but  the DBCP jar (which is packaged with Tomcat) is being loaded by a 
different class loader to the one that loads your servlet.  This is 
causing some problem with the system property "jdbc.drivers" that you're

setting in your servlet (although the exact problem is not clear to me.)

I made sure the MySql driver was being loaded by the same class loader 
as that which loaded the DriverManagerConnectionFactory with the line:
Class.forName("com.mysql.jdbc.Driver", true, 
DriverManagerConnectionFactory.class.getClassLoader());

and it all worked.

I'm not convinced I know the reasons why it worked, if anyone could 
enlighten me it would be much appreciated.

Graeme

Clark D. Richey, Jr. wrote:

>When using the following code I get this error:
>
>org.apache.commons.dbcp.DbcpException: java.sql.SQLException: No
>suitable driver
>
>I have the jar file with the mysql driver in the common/lib directory.
I
>have also tried placing it in the web-inf/lib directory with no better
>results. Help please!
>
> 
>
>package org.jugaccino.servlet;
>
> 
>
>import java.io.IOException;
>
>import java.io.PrintWriter;
>
>import java.sql.*;
>
>import javax.servlet.ServletConfig;
>
>import javax.servlet.ServletException;
>
>import javax.servlet.http.*;
>
>import org.apache.commons.dbcp.*;
>
>import org.apache.commons.pool.impl.GenericObjectPool;
>
> 
>
>public class DriverTestMaual extends HttpServlet
>
>{
>
> 
>
>    public DriverTestMaual()
>
>    {
>
>    }
>
> 
>
>    public void init(ServletConfig config)
>
>        throws ServletException
>
>    {
>
>        super.init(config);
>
>    }
>
> 
>
>    public void destroy()
>
>    {
>
>    }
>
> 
>
>    protected void processRequest(HttpServletRequest request,
>HttpServletResponse response)
>
>        throws ServletException, IOException
>
>    {
>
>        response.setContentType("text/html");
>
>        PrintWriter out = response.getWriter();
>
>        System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
>
>        try
>
>        {
>
>            org.apache.commons.pool.ObjectPool connectionPool = new
>GenericObjectPool(null);
>
>org.apache.commons.dbcp.ConnectionFactory connectionFactory = new
>DriverManagerConnectionFactory("jdbc:mysql://localhost/jugaccino?user=x
x
>x&password=xxx;", null);
>
>            PoolableConnectionFactory poolableConnectionFactory = new
>PoolableConnectionFactory(connectionFactory, connectionPool, null,
null,
>false, true);
>
>            PoolingDriver driver = new PoolingDriver();
>
>            driver.registerPool("jugaccino", connectionPool);
>
>        }
>
>        catch(Exception ex)
>
>        {
>
>            ex.printStackTrace();
>
>            out.println(ex);
>
>            return;
>
>        }
>
>        Connection conn = null;
>
>        Statement stmt = null;
>
>        ResultSet rset = null;
>
>        try
>
>        {
>
>            out.println("Creating connection.");
>
>            conn =
>DriverManager.getConnection("jdbc:apache:commons:dbcp:jugaccino");
>
>            out.println("Creating statement.");
>
>            stmt = conn.createStatement();
>
>            out.println("Executing statement.");
>
>            rset = stmt.executeQuery("select downloads from
>downloadcount");
>
>            out.println("Results:");
>
>            if(rset.next())
>
>                out.println(rset.getInt("downloads"));
>
>        }
>
>        catch(SQLException e)
>
>        {
>
>            e.printStackTrace();
>
>            out.println(e.getMessage());
>
>        }
>
>        finally
>
>        {
>
>            try
>
>            {
>
>                rset.close();
>
>            }
>
>            catch(Exception e) { }
>
>            try
>
>            {
>
>                stmt.close();
>
>            }
>
>            catch(Exception e) { }
>
>            try
>
>            {
>
>                conn.close();
>
>            }
>
>            catch(Exception e) { }
>
>        }
>
>        out.close();
>
>    }
>
> 
>
>    protected void doGet(HttpServletRequest request,
HttpServletResponse
>response)
>
>        throws ServletException, IOException
>
>    {
>
>        processRequest(request, response);
>
>    }
>
> 
>
>    protected void doPost(HttpServletRequest request,
>HttpServletResponse response)
>
>        throws ServletException, IOException
>
>    {
>
>        processRequest(request, response);
>
>    }
>
> 
>
>    public String getServletInfo()
>
>    {
>
>        return "Short description";
>
>    }
>
>}
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org





Mime
View raw message