连接数据库出错:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

Java Web 同时被 3 个专栏收录
17 篇文章 3 订阅
11 篇文章 3 订阅
2 篇文章 1 订阅

连接数据库出错

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

今天在做Java web项目,在进行数据库连接的时候运行后报错如下:

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)

出现以上错误提示表示数据库连接失败了

你没看错

就是辛辛苦苦要连接的数据库没连接上

在这里插入图片描述

报错原因看一下

是:未找到数据库驱动

怎么会没有数据库驱动???

那么数据库驱动到底在哪里呢?

在这里插入图片描述

首先,要想连接数据库,要先下载用于数据库连接的.jar包

这里给出Mysql数据连接的.jar包下载地址:

数据库连接jar包下载地址

进去后找到自己的开发语言的相应jar包下载即可

下载好jar包后,我们打开看【以下以Java的jar包为例】

如图:

在这里插入图片描述
按图示路径点进去即可看到用于数据库连接的驱动

Driver.class
在这里插入图片描述
既然数据库连接驱动在jar包里找到了

那就解决问题吧

出现上述报错的原因通常有三种:

  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

【特别注意】如果以上错误都不是,还有一种常见错误,就是jre1.8.0_162\lib\ext目录下没有加入jar包,解决办法很简单,将mysql-connector-java.jar包复制到
C:\Program Files\Java\jre1.8.0_162\lib\ext目录下即可

重新启动服务

运行程序

就可以成功连接上数据库啦~~

在这里插入图片描述

评论 26 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:点我我会动 设计师:我叫白小胖 返回首页

打赏作者

奋斗中的编程菜鸟

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值