SQLException: No suitable driver found

80,402

Solution 1

java.sql.SQLException: No suitable driver found

This exception can have 2 causes:

  1. The JDBC driver is not loaded at all.
  2. URL does not match any of the loaded JDBC drivers.

Since the driver seems to be loaded (although in an incorrect manner), it look like that the URL is plain wrong. Ensure that the value of your url variable matches the following format

jdbc:mysql://localhost:3306/dbname

See also:


Unrelated to the concrete problem: Java code doesn't belong in a JSP file. Work on that as well. Your exception handling is also terrible, you should throw the exception (so that it blocks executing the remnant of the code) instead of printing the message/trace and then continue with the code.

Solution 2

I had to remove mysql-connector-java-*.jar from WEB-INF/lib and add it into tomcat6/lib folder. (tomcat6 is where tomcat was installed.) I do not know why this worked but it worked for me.

Solution 3

Did you register your class with the Driver? For example:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.getConnection(url,user,password);

Solution 4

Are you passing an emtpy url string to getConnection()? The error message starts out

No suitable driver found for ERROR

No suitable driver found for [blank]? Seems like you're not passing a url here.

Share:
80,402
Eliezer
Author by

Eliezer

Software engineer pursuing a MS in computer science. Currently focused on Android, but I've been known to get involved with .NET, Python (App Engine), and C++.

Updated on July 09, 2022

Comments

  • Eliezer
    Eliezer almost 2 years

    I have a Java class that accesses a MySQL database through JDBC that I use in a JSP running on Tomcat, and I am getting No Driver Found Exception.

    I have a method:

    private static Statement makeStatement() {
       try{
        com.mysql.jdbc.Driver d = null;
        try{d = new com.mysql.jdbc.Driver();}catch(Exception e){
          System.out.println("ERROR BY NEW DRIVER " + e.toString() + 
          "\n");e.printStackTrace();}
        Connection con = DriverManager.getConnection(url, user, password);
        return con.createStatement();
       }catch(java.sql.SQLException ex){
          System.out.println("ERROR IN makeStatement " + "\nERROR - " +
          ex.toString() +  "\n ERROR CODE:\n " + ex.getErrorCode() + 
          "\nSQLSTATE:\n " + ex.getSQLStat        e());ex.printStackTrace();}
        return null;
     }
    

    That throws an error at Connection con = DriverManager.getConnection(url, user, password); Here is my printout from catalina.out:

      Received Parameters
    
    ERROR IN makeStatement 
    ERROR - java.sql.SQLException: No suitable driver found for 
     ERROR CODE:
     0
    SQLSTATE:
     08001
    java.sql.SQLException: No suitable driver found for 
            at java.sql.DriverManager.getConnection(DriverManager.java:602)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at message.Message.makeStatement(Message.java:72)
            at message.Message.query(Message.java:79)
            at message.Message.getName(Message.java:225)
            at org.apache.jsp.message_jsp._jspService(message_jsp.java:288)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:548)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
            at java.lang.Thread.run(Thread.java:619)
    ERROR IN QUERY java.lang.NullPointerException
    ERROR in getName java.lang.NullPointerException
    -----------------------------------
    
      Received Parameters
    
            newMessage      =  
            newpassword     = 
            verifypassword  = 
            sendinfo        = 
            username        = Eli
            newusername     = 
            login   = login
            password        = tree
    ERROR IN makeStatement 
    ERROR - java.sql.SQLException: No suitable driver found for 
     ERROR CODE:
     0
    SQLSTATE:
     08001
    java.sql.SQLException: No suitable driver found for 
            at java.sql.DriverManager.getConnection(DriverManager.java:602)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at message.Message.makeStatement(Message.java:72)
            at message.Message.query(Message.java:79)
            at message.Message.validUser(Message.java:195)
            at org.apache.jsp.message_jsp._jspService(message_jsp.java:123)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:548)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
            at java.lang.Thread.run(Thread.java:619)
    ERROR IN QUERY java.lang.NullPointerException
    ERROR in validUser java.lang.NullPointerException
    ERROR IN makeStatement 
    ERROR - java.sql.SQLException: No suitable driver found for 
     ERROR CODE:
     0
    SQLSTATE:
     08001
    java.sql.SQLException: No suitable driver found for 
            at java.sql.DriverManager.getConnection(DriverManager.java:602)
            at java.sql.DriverManager.getConnection(DriverManager.java:185)
            at message.Message.makeStatement(Message.java:72)
            at message.Message.query(Message.java:79)
            at message.Message.getName(Message.java:225)
            at org.apache.jsp.message_jsp._jspService(message_jsp.java:288)
            at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
            at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
            at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:548)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
            at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
            at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
            at java.lang.Thread.run(Thread.java:619)
    ERROR IN QUERY java.lang.NullPointerException
    ERROR in getName java.lang.NullPointerException
    

    The only error that matters is the one in makeStatement() as that failing causes all the other errors. I have quadruple checked that I have the correct jar files in my WEB-INF/lib directory and I have restarted Tomcat more times than I would ever want to. I have a separate webapp that uses makeStatement() in a different .java file, and that webapp runs fine. Even weirder is that I have this in the .java:

    static {
      System.err.println("\n\nTEST MYSQL ACCESS: dump all relevant tables:");
      dump();
      System.err.println("END OF MYSQL ACCESS ACCESS.");
    }
    
     public static void dump() {
       try {
         readUsers();
         for (UserRecord u: users)
         System.err.println(u.username+" "+u.password);
       } catch (Exception e) {System.err.println(e);}
     }
    

    where readUsers() reads all the users from the database using makeStatement(). This actually works and all the users in the database are printed (not shown here for obvious reasons :) ) and then the driver not found error occurs.

  • Eliezer
    Eliezer about 13 years
    I did find that a bit odd, but the URL is declared in an init function to be this: url = "jdbc:mysql://localhost:"+port+"/"+user;
  • Eliezer
    Eliezer about 13 years
    The URL is declared in an init function to be this: url = "jdbc:mysql://localhost:"+port+"/"+user;
  • BalusC
    BalusC about 13 years
    Do a System.out.println("The URL is: " + url); right before the DriverManager.getConnection(url, user, password) call. My cents on that it is not what you think it is.
  • Eliezer
    Eliezer about 13 years
    This code isn't in a JSP. It's in a .java that I compiled and stuck in a jar in the jsps WEB-INF/lib directory. I'm not that great with error handling; it's one of the things I need to brush up on
  • Eliezer
    Eliezer about 13 years
    You're right. The URL is empty after the first time makeStatement() is called. Now to figure out why...
  • BalusC
    BalusC about 13 years
    Just run a debugger so that you can execute code line by line and explore all variables in the stack.
  • Eliezer
    Eliezer about 13 years
    Found the problem. Thanks so much
  • BalusC
    BalusC about 13 years
    The new Driver() does that already. This has however the huge disadvantage that the JDBC driver is now also a compiletime dependency instead of alone a runtime dependency (in other words: the code is not very portable/reusable). That's why it's recommended to load JDBC drivers by Class#forName() instead of plain importing/instantiating them. For technical background information, see stackoverflow.com/questions/2092659/…
  • BalusC
    BalusC about 13 years
    @John: I'll bet an accident url = ""; somewhere in the code or it is actually not properly initialized :)
  • John Kane
    John Kane about 13 years
    Thanks for saying that. I have always just registered the class with the driver manager and did not even look into instantiating the driver itself.
  • John Kane
    John Kane about 13 years
    yeah, I was thinking that too. I was just thinking that it would be good to post the solution in case anyone else came across this with a similar problem.
  • BalusC
    BalusC about 13 years
    @John: at any way, it boiled down to that the URL was plain wrong :)
  • Eliezer
    Eliezer about 13 years
    @John It was more of an issue where I set the URL in a method that I stopped using that was getting called in the static block. I just didn't put 2 and 2 together to realize that the method wasn't called anymore, but when when I saw that the URL was "" I started looking to see why it wasn't getting initialized. So just negligence :)
  • user35443
    user35443 almost 11 years
    Can you please comment your solution a bit more?
  • Code cracker
    Code cracker over 9 years
    can u please improve your question by screen shots or better formatting
  • Eliezer
    Eliezer over 9 years
    This question was answered correctly 3 1/2 years agp.