开发者社区> 问答> 正文

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

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

我有一个方法:

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 唯一重要的错误是一个,makeStatement()因为失败会导致所有其他错误。我已四重检查我的WEB-INF / lib目录中是否具有正确的jar文件,并且重新启动Tomcat的次数比我想要的要多。我有一个makeStatement()在不同的.java文件中使用的单独的webapp,该webapp运行良好。更奇怪的是,我在.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()。这实际上可以正常工作,并且将打印数据库中的所有用户(出于明显的原因未在此处显示:)),然后发生驱动程序未找到错误。

展开
收起
保持可爱mmm 2020-05-17 10:56:15 499 0
1 条回答
写回答
取消 提交回答
  • java.sql.SQLException:找不到合适的驱动程序

    此异常可能有2个原因:

    完全不加载JDBC驱动程序。 URL与任何已加载的JDBC驱动程序都不匹配。 由于驱动程序似乎已加载(尽管以错误的方式),因此该URL显然是错误的。确保url变量的值与以下格式匹配

    jdbc:mysql://localhost:3306/dbname 也可以看看: 将Java连接到MySQL数据库 与具体问题无关:Java代码不属于JSP文件。也要努力。您的异常处理也很糟糕,您应该抛出异常(以使其阻止执行代码的剩余部分),而不是打印消息/跟踪,然后继续执行代码。来源:stack overflow

    2020-05-17 11:09:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像