已解决No suitable driver found for jdbc:mysql://localhost:3306/ 问题
一、Bug描述
在学习java数据库连接池使用的时候遇到问题,无法连接到数据库,查看日志是"No Suitable Driver Found For Jdbc",但查看数据库连接配置没问题。
代码语言:javascript复制java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/db_mydb?characterEncoding=utf8&useSSL=false
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.luojia.sims.utils.DBManager.getConn(DBManager.java:30)
at com.luojia.sims.dao.UserDAO.addUser(UserDAO.java:22)
at org.apache.jsp.user_005fsave_jsp._jspService(user_005fsave_jsp.java:135)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:500)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
四月 08, 2018 8:07:34 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/sims] threw exception [An exception occurred processing [user_save.jsp] at line [23]
20:
21: //璋冪敤UserDAO涓殑addUser鏂规硶瀹屾垚淇濆瓨
22: UserDTO u = new UserDTO(0,name,pwd,sex,age,remark);
23: boolean b = new UserDAO().addUser(u);
24:
25: //鏍规嵁娉ㄥ唽鐘舵?佽繘琛岃烦杞?
26: if(b){
Stacktrace:] with root cause
java.lang.NullPointerException
at com.luojia.sims.dao.UserDAO.addUser(UserDAO.java:24)
at org.apache.jsp.user_005fsave_jsp._jspService(user_005fsave_jsp.java:135)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:500)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
这个问题可把我愁坏了,要不问一下GPT?上。
二、定位报错点及原因
问了一下GPT,得到的答案是这样的。 简单的来说,就是可能驱动程序有问题,或者是URL的问题。
.
三、最终的解决方案
首先,下载数据库链接的jar包:https://dev.mysql.com/downloads/connector/
进去之后找到对应好的一个jar包下载。
下载好了之后,按照路径进去,可以看到用于 数据库链接的驱动 Driver.class
出现上述报错的原因通常有三种:
1、URL格式不对:正确为 Connectionconn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/XX”,“root”,“XXXX”)
2、驱动字符串出错(com.mysql.jdbc.Driver)
3、Classpath中没有加入合适的mysql_jdbc驱动
那么对应的解决方案如下:
1、书写正确的URL格式
2、书写正确的驱动字符串
3、将驱动加入CLASSPATH
四、相关注意事项
总结来说解决方案如下:
代码语言:javascript复制1, 连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/XX","root","XXXX")
2, 驱动字符串出错(com.mysql.jdbc.Driver)
3, Classpath中没有加入合适的mysql_jdbc驱动
4, 尝试着将mysql-connector-java-5.1.34-bin.jar的jar包加入C:Program FilesJavajre1.6.0_02libext文件夹下(或者直接导入项目中),问题解决
5, 检查是不是jdbc:mysql://写成了jdbc:mysql// 连接写不对也会报这个找不到驱动的错
如果以上错误都不是,还有一种常见错误,就是jre1.8.0_162libext目录下没有加入jar包,解决办法很简单,将mysql-connector-java.jar包复制到 C:Program FilesJavajre1.8.0_162libext 目录下即可