jsp页面连接数据库失败

J2EE 码拜 10年前 (2015-05-10) 775次浏览 0个评论
 

数据库是mysql,TOMCAT的\conf\Catalina\localhost\dd.xml配置如下:
<?xml version=”1.0″ encoding=”GBK”?>
<Context docBase=”D:\work\codes\01\aa” debug=”0″ privileged=”true”>
   <Resource name=”jdbc/dstest” auth=”Container” type=”javax.sql.DataSource”
               maxActive=”5″ maxIdle=”2″ maxWait=”10000″
               username=”root” password=”root”
   driverClassName=”com.mysql.jdbc.Driver”
               url=”jdbc:mysql://localhost:8088/j2ee”/>
</Context>
——————————————————————————–
JSP页面报错如下:
HTTP Status 500 – 

——————————————————————————–

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

Last packet sent to the server was 0 ms ago.)
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause 

javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

Last packet sent to the server was 0 ms ago.)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.tomcatTest_jsp._jspService(tomcatTest_jsp.java:94)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause 

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

Last packet sent to the server was 0 ms ago.)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
org.apache.jsp.tomcatTest_jsp._jspService(tomcatTest_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
org.apache.jsp.tomcatTest_jsp._jspService(tomcatTest_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
org.apache.jsp.tomcatTest_jsp._jspService(tomcatTest_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
……………

自己顶下- –
不太懂      帮顶
10分
JSP连接数据库哪里用配置TOMCAT。我写一段代码给你参考一下:
<%
String dbUrl=”jdbc:mysql://localhost:8088/j2ee”;
String userName=”root”;
String password=”root”;
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn = DriverManager(dbUrl,userName,password);
%>
在JSP文件开头先要导入java.sql.*
没用过 jsp连接数据库
10分
楼主是不是没把mysql-connector包导入到lib文件夹下面?

这个异常可能是mysql里面出的问题,楼主最好把连接数据库的代码贴一下。

10分
数据源连接池的用法需要两个包:
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
你确定你的MySQL的端口号是8088吗?
呵呵呵,不清楚了
自己查下原因:
 1. 保存自己写的配置文件Driver,UserName,Password,没有写错.
 2. 数据库存在吗?
 3. 配置文件之间的关系正确吗?
 4. 有权限连接吗?
引用 9 楼 east271536394 的回复:

自己查下原因:
 1. 保存自己写的配置文件Driver,UserName,Password,没有写错.
 2. 数据库存在吗?
 3. 配置文件之间的关系正确吗?
 4. 有权限连接吗?

顶。。。
难道是有名的mysql 8小时断开连接问题??
引用 7 楼 xiaoloveliuforever 的回复:

你确定你的MySQL的端口号是8088吗?

mysql端口默认
8080

在JSP  链接数据库  不符合MVC 吧 JSP  链接数据库  可以用jstl 标签

好像是缺少dbcpjar包呢
kht kh 路过。。。接分
路过。。。。。。。。。。。。。。。。。。。。。。
引用 6 楼 licip 的回复:

数据源连接池的用法需要两个包:
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar

Cannot create PoolableConnectionFactory (Communications link failure
包加了嘛


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明jsp页面连接数据库失败
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!