连接数据库出错
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=falseat 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..NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)at org.apache..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, 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 causejava.lang.NullPointerExceptionat 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..NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)at org.apache..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包里找到了
那就解决问题吧
出现上述报错的原因通常有三种:
URL格式不对:正确为
Connectionconn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/XX”,“root”,“XXXX”)
驱动字符串出错(com.mysql.jdbc.Driver)
Classpath中没有加入合适的mysql_jdbc驱动
对于前两种错误是数据库连接语句上的问题,第三种是环境变量的配置问题
对应解决方法:
书写正确的URL格式
书写正确的驱动字符串
将驱动加入CLASSPATH
【特别注意】如果以上错误都不是,还有一种常见错误,就是jre1.8.0_162\lib\ext目录下没有加入jar包,解决办法很简单,将mysql-connector-java.jar包复制到
C:\Program Files\Java\jre1.8.0_162\lib\ext目录下即可
重新启动服务
运行程序
就可以成功连接上数据库啦~~