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

我有一个Java类,通过JDBC访问MySQL数据库,在Tomcat上运行的JSP中使用,我得到没有驱动程序发现exception。

我有一个方法:

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);抛出一个错误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比我想要的更多。 我有一个单独的web应用程序,使用不同的.java文件中的makeStatement() ,并且该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()从数据库读取所有用户。 这实际上工作和数据库中的所有用户打印(这里没有显示原因:)),然后驱动程序未find错误发生。

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

这个例外可能有两个原因:

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

由于驱动程序似乎被加载(虽然方式不正确),但看起来像这个URL是错误的。 确保你的urlvariables的值符合以下格式

 jdbc:mysql://localhost:3306/dbname 

也可以看看:

  • 将Java连接到MySQL数据库

具体问题无关 :Java代码不属于JSP文件。 继续努力。 你的exception处理也是可怕的,你应该抛出exception(以便阻止执行代码的剩余部分),而不是打印消息/跟踪,然后继续执行代码。

你是否注册了驾驶员的课程? 例如:

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

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

你是否传递一个静态的urlstringgetConnection()? 错误信息开始

 No suitable driver found for ERROR 

没有合适的驱动程序find[空白]? 好像你没有在这里传递一个url。

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