这个问题已经在这里有了答案 :
臭名昭著的java.sql.SQLException:未找到合适的驱动程序 (13个答案)
4年前关闭。
我有一个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的打印输出”上引发错误:
Connection con = DriverManager.getConnection(url, user, password);
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中有这个:
makeStatement()
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()。这实际上可以正常工作,并且将打印数据库中的所有用户(出于明显的原因未在此处显示:)),然后发生驱动程序未找到错误。
readUsers()
java.sql.SQLException:找不到合适的驱动程序
此异常可能有2个原因:
由于驱动程序似乎已加载(尽管以错误的方式),因此该URL显然是错误的。确保url变量的值与以下格式匹配
url
jdbc:mysql://localhost:3306/dbname
与 具体问题 无关 :Java代码不属于JSP文件。也要努力。您的异常处理也很糟糕,您应该 抛出 异常(以使其阻止执行代码的剩余部分),而不是打印消息/跟踪,然后继续执行代码。