You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Clark D. Richey, Jr." <cr...@speakeasy.net> on 2003/06/27 03:47:45 UTC

no suitable driver when using DBCP

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=xx
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";

    }

}


RE: no suitable driver when using DBCP

Posted by "Clark D. Richey, Jr." <cr...@speakeasy.net>.
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





Re: no suitable driver when using DBCP

Posted by Graeme Rimmer <gr...@lineone.net>.
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=xx
>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";
>
>    }
>
>}
>
>
>  
>


RE: no suitable driver when using DBCP

Posted by "Clark D. Richey, Jr." <cr...@speakeasy.net>.
That did not solve the problem.

-----Original Message-----
From: Keith Veleba [mailto:keith@veleba.net] 
Sent: Friday, June 27, 2003 6:58 AM
To: Jakarta Commons Users List
Subject: Re: no suitable driver when using DBCP

Having it in the CLASSPATH and the system properties isn't enough.  You

also have to call DriverManager.registerDriver() for the driver you  
want to use.


On Thursday, June 26, 2003, at 09:47  PM, 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=xx
> 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





Re: no suitable driver when using DBCP

Posted by Keith Veleba <ke...@veleba.net>.
Having it in the CLASSPATH and the system properties isn't enough.  You  
also have to call DriverManager.registerDriver() for the driver you  
want to use.


On Thursday, June 26, 2003, at 09:47  PM, 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=xx
> 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";
>
>     }
>
> }
>