That message indicates a class not found problem.  So, for the connection
string (URL), and more specifically, the jdbc:mysql: portion of it, no
driver was registered or the specified driver 9if registered) was not found.
I.e., the "mysql" portion needs somehow to be registered with the driver
class com.mysql.jdbc.Driver.

I wrote a ServletContextListener which pre-loads all the drivers I need,
using a method like the following:

public class ApplicationLifecycleListener implements ServletContextListener 
{
    private final static Log logger =
LogFactory.getLog(ApplicationLifecycleListener.class);

    public void contextInitialized(ServletContextEvent sce) 
    {
        logger.info("Application context initialized.");
        loadAllDrivers();
    }
    private void loadAllDrivers()
    {
        try {
            ResourceBundle bundle =
ResourceBundle.getBundle(ApplicationLifecycleListener.class.getName());
            Enumeration keyEnumerator = bundle.getKeys();
            while (keyEnumerator.hasMoreElements()) {
                final String key = (String)keyEnumerator.nextElement();
                final String value = bundle.getString(key);
                if (key.startsWith("loadJDBCDriver")) {
                    try {
                        Class.forName(value).newInstance();
                        logger.debug("Loaded JDBCDriver for " + key + "=" +
value);
                    }
                    catch (Exception e) {
                        logger.error("While processing property " + key +
"=" + value, e);
                    }
                }
            }
        }
        catch (MissingResourceException mre) {
            logger.warn("No resource bundle for " + getClass().getName() + "
- ignoring");
        }
    }
...
}


ApplicationLifecycleListener.properties:
# all drivers enumerated with a key starting with "loadJDBCDriver" will 
# be automatically loaded during application startup.
loadJDBCDriver.mysql=com.mysql.jdbc.Driver


It fixes exactly this problem.

HTH,
Tim


-----Original Message-----
From: Jay Wallhaus [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 16, 2006 2:49 PM
To: users@tomcat.apache.org
Subject: No Suitable Driver

I keep getting the following error:

javax.servlet.ServletException: Unable to get connection, DataSource
invalid: "java.sql.SQLException: No suitable driver"
 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(
PageContextImpl.java :825)
 org.apache.jasper.runtime.PageContextImpl.handlePageException(
PageContextImpl.java:758)
 org.apache.jsp.TryDB_jsp._jspService(TryDB_jsp.java:113)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java :94)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java
:324)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java :292)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I am using Tomcat 5.0.28, JSDK 1.4.2_08 and MySQL 5.0.20a.

Here is a list of what I can think of that I have tried and nothing resolves
the "no suitable driver" error:
1) Made sure that mysql-connector-java-3.1.12-bin.jar contains the JDBC
driver.
2) I have tried putting the MySQL JAR ( mysql-connector-java-3.1.12-bin.jar)
in WEB-INF/lib, common/lib and shared/lib.
3) I have tried putting the MySQL JAR ZIP file
mysql-connector-java-3.1.12.zip in WEB-INF/lib, common/lib and shared/lib
4) I have placed "<% Class.forName("com.mysql.jdbc.Driver"); %>" in the JSP
page.
5) I changed localhost in the URL connection string to 127.0.0.1.
6) I added the MySQL server port 3306 to the URL connection string.
7) I have tried using another MySQL Jar
mysql-connector-java-5.0.0-beta-bin.jar.
8) I have completely uninstalled Tomcat 5.0.28 and reinstalled it.
9) I have completely uninstalled MySQL and reinstalled it, resetting up the
database.
10) I have triple checked the database privileges, login ID (wroxuser),
password (wrox), table (products).

Any suggestions for resolving the "No suitable driver" problem would be
greatly appreciated.


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

Reply via email to