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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Eliezer
  • 7,209
  • 12
  • 56
  • 103

5 Answers5

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The URL is declared in an init function to be this: `url = "jdbc:mysql://localhost:"+port+"/"+user;` – Eliezer May 12 '11 at 19:02
  • 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. – BalusC May 12 '11 at 19:05
  • 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 May 12 '11 at 19:06
  • You're right. The URL is empty after the first time `makeStatement()` is called. Now to figure out why... – Eliezer May 12 '11 at 19:13
  • Just run a debugger so that you can execute code line by line and explore all variables in the stack. – BalusC May 12 '11 at 19:15
  • Found the problem. Thanks so much – Eliezer May 12 '11 at 19:16
  • @John: I'll bet an accident `url = "";` somewhere in the code or it is **actually** not properly initialized :) – BalusC May 12 '11 at 19:21
  • 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. – John Kane May 12 '11 at 19:25
  • @John: at any way, it boiled down to that the URL was plain wrong :) – BalusC May 12 '11 at 19:27
  • @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 :) – Eliezer May 13 '11 at 18:38
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.

Parag Sarda
  • 1,576
  • 1
  • 13
  • 5
2

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

Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.getConnection(url,user,password);
John Kane
  • 4,383
  • 1
  • 24
  • 42
  • 1
    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 http://stackoverflow.com/questions/2092659/what-is-difference-between-class-forname-and-class-forname-newinstance/2093236#2093236 – BalusC May 12 '11 at 19:20
  • 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 May 12 '11 at 19:24
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.

heavi5ide
  • 1,599
  • 10
  • 11
  • 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 May 12 '11 at 19:01
-2

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