-2
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)

我的 Util 文件如下:

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)
           {
           }
       }
   }
}

我已将 MySQL 连接器放入 buildpath 以及 web-inf 的 lib 文件夹中。为什么我仍然收到此错误?

4

2 回答 2

5

您需要加载com.mysql.jdbc.Driver驱动程序类。

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

查看官方文档

在 MySQL Connector/J 中实现 java.sql.Driver 的类的名称是 com.mysql.jdbc.Driver。org.gjt.mm.mysql.Driver 类名也可用于向后兼容 MM.MySQL,即 Connector/J 的前身。在注册驱动程序时使用此类名称,或者在以其他方式配置软件以使用 MySQL Connector/J 时。

于 2013-07-28T12:39:42.967 回答
1

这是一个相当老的线程,所以我只是应该补充一点,“com.mysql.jdbc.Driver”已被弃用,所以你需要使用“com.mysql.cj.jdbc.Driver”

于 2018-12-17T18:44:22.877 回答