SQLException: No suitable driver found [duplicate]
Asked Answered
L

5

11

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.

Luciusluck answered 12/5, 2011 at 18:32 Comment(0)
D
17

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.

Deenadeenya answered 12/5, 2011 at 19:1 Comment(10)
The URL is declared in an init function to be this: url = "jdbc:mysql://localhost:"+port+"/"+user;Luciusluck
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.Deenadeenya
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 onLuciusluck
You're right. The URL is empty after the first time makeStatement() is called. Now to figure out why...Luciusluck
Just run a debugger so that you can execute code line by line and explore all variables in the stack.Deenadeenya
Found the problem. Thanks so muchLuciusluck
@John: I'll bet an accident url = ""; somewhere in the code or it is actually not properly initialized :)Deenadeenya
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.Charis
@John: at any way, it boiled down to that the URL was plain wrong :)Deenadeenya
@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 :)Luciusluck
S
3

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.

Sosthena answered 10/8, 2013 at 6:14 Comment(1)
Can you please comment your solution a bit more?Affirmative
C
2

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

Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.getConnection(url,user,password);
Charis answered 12/5, 2011 at 19:2 Comment(2)
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 #2093159Deenadeenya
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.Charis
L
1

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.

Longley answered 12/5, 2011 at 18:56 Comment(1)
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;Luciusluck
P
-2

Go to library folder of your project and right click it,then go to 'Add Library' option.Now add the mysql.jdbc.Driver

Pohl answered 19/12, 2014 at 6:21 Comment(2)
can u please improve your question by screen shots or better formattingNewton
This question was answered correctly 3 1/2 years agp.Luciusluck

© 2022 - 2024 — McMap. All rights reserved.