Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - Why am I getting "No suitable driver found for jdbc:mysql://localhost:3306/test2"?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...