SQLException:找不到合适的驱动程序

发布于 2024-11-06 15:26:26 字数 9473 浏览 5 评论 0原文

我有一个通过 JDBC 访问 MySQL 数据库的 Java 类,我在 Tomcat 上运行的 JSP 中使用该类,但我收到了“未找到驱动程序”异常。

我有一个方法:

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;
 }

Connection con = DriverManager.getConnection(url, user, password); 处引发错误 这是我从 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

唯一重要的错误是 < code>makeStatement() 因为失败会导致所有其他错误。我已经四次检查了 WEB-INF/lib 目录中是否有正确的 jar 文件,并且重新启动 Tomcat 的次数比我想要的次数要多。我有一个单独的 web 应用程序,它在不同的 .java 文件中使用 makeStatement() ,并且该 web 应用程序运行良好。更奇怪的是,我在 .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);}
 }

其中 readUsers() 使用 makeStatement() 从数据库读取所有用户。这实际上是有效的,并且数据库中的所有用户都被打印(出于明显的原因,此处未显示:)),然后出现驱动程序未找到错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-11-13 15:26:26

java.sql.SQLException:找不到合适的驱动程序

此异常可能有 2 个原因:

  1. 根本未加载 JDBC 驱动程序。
  2. URL 与任何加载的 JDBC 驱动程序都不匹配。

由于驱动程序似乎已加载(尽管以不正确的方式),因此看起来 URL 完全错误。确保 url 变量的值与以下格式匹配

jdbc:mysql://localhost:3306/dbname

另请参阅:


与具体问题无关:Java 代码不属于 JSP 文件。也在这方面努力。您的异常处理也很糟糕,您应该抛出异常(以便它阻止执行代码的剩余部分),而不是打印消息/跟踪,然后继续执行代码。

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.

好多鱼好多余 2024-11-13 15:26:26

我必须从 WEB-INF/lib 中删除 mysql-connector-java-*.jar 并将其添加到 tomcat6/lib 文件夹中。 (tomcat6 是安装 tomcat 的地方。)我不知道为什么这有效,但它对我有用。

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.

浅沫记忆 2024-11-13 15:26:26

您向司机注册了您的课程吗?例如:

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

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

Class.forName("net.sourceforge.jtds.jdbc.Driver");
DriverManager.getConnection(url,user,password);
初见终念 2024-11-13 15:26:26

您是否将空 url 字符串传递给 getConnection()?错误消息开始

No suitable driver found for ERROR

没有找到适合 [blank] 的驱动程序?看来您没有在这里传递网址。

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.

天气好吗我好吗 2024-11-13 15:26:26

转到项目的库文件夹并右键单击它,然后转到“添加库”选项。现在添加 mysql.jdbc.Driver

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文