Why am I getting “No suitable driver found for jdb

2019-09-23 07:15发布

问题:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test2
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.room302.util.DBConnectionUtil.getConnection(DBConnectionUtil.java:32)
    at com.room302.dao.roomDAO.addexpense(roomDAO.java:26)
    at com.room302.servlet.Navigator.doPost(Navigator.java:69)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

My Util file is as follows:

public class DBConnectionUtil
{   
    private static final String USERNAME="root";
    private static final String PASSWORD="1234";
    private static final String URL="jdbc:mysql://localhost:3306/test2";
    private static final String DRIVER_NAME="mysql";

    private static Connection conn=null;

    static
    {
        try
        {
            System.out.println("searching for driver");
            Class.forName(DRIVER_NAME);
        }
        catch(ClassNotFoundException cnfe)
        {                               
        }
    }

    public static Connection getConnection() throws SQLException
    {           
        conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);                      
        return conn;
    }

   public static void closeConnection(Connection conn)
   {
       if(conn!=null)
       {
           try
           {
               conn.close();
           }
           catch (Exception e)
           {
           }
       }
   }
}

I have put MySQL connector in the buildpath and also in lib folder of web-inf. Why am I still getting this error?

回答1:

You need to load the com.mysql.jdbc.Driver driver class.

private static final String DRIVER_NAME="com.mysql.jdbc.Driver";

Look at the official documentation:

The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver. The org.gjt.mm.mysql.Driver class name is also usable for backward compatibility with MM.MySQL, the predecessor of Connector/J. Use this class name when registering the driver, or when otherwise configuring software to use MySQL Connector/J.



回答2:

This is a fairly old thread so I just though I should add that "com.mysql.jdbc.Driver" has been deprecated so you need to use "com.mysql.cj.jdbc.Driver"