tomcat连不上数据库mysql_在使用tomcat时数据库连接不上

我用的是Spring+Mybatis,配置文件写的没问题,昨天也连上了,今天就一直报错严重:Servlet.service()forservlet[springmvc]incontextwithpath[/help]threwexception[Requestprocessin...

我用的是Spring+Mybatis,配置文件写的没问题,昨天也连上了,今天就一直报错

严重: Servlet.service() for servlet [springmvc] in context with path [/help] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io 异常: The Network Adapter could not establish the connection)

### The error may exist in file [E:\tomcat\apache-tomcat-7.0.47\apache-tomcat-7.0.47\webapps\help\WEB-INF\classes\org\xxxx\entity\Personl.xml]

### The error may involve org.xxxx.dao.PersonDao.findAll

### The error occurred while executing a query

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io 异常: The Network Adapter could not establish the connection)] with root cause

java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection

at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:74)

at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:494)

at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)

at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:490)

at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:202)

at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tomcat是一个开源的Java Servlet容器,它可以用于运行Java Web应用程序。要在Tomcat中连接MySQL数据库,你需要完成以下几个步骤: 1. 下载并安装MySQL数据库:首先,你需要从MySQL官方网站下载并安装MySQL数据库服务器。根据你的操作系统选择适合的版本,并按照安装向导进行安装。 2. 下载并配置JDBC驱动程序:Tomcat需要使用JDBC驱动程序来连接MySQL数据库。你可以从MySQL官方网站下载适合你的MySQL JDBC驱动程序(通常是一个JAR文件)。将该驱动程序的JAR文件复制到Tomcat的lib目录下。 3. 在Tomcat中配置数据源:打开Tomcat的conf目录下的context.xml文件,在<Context>标签内添加以下内容: ```xml <Resource name="jdbc/yourDBName" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="yourUsername" password="yourPassword" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/yourDBName"/> ``` 将上述代码中的yourDBName替换为你的数据库名称,yourUsername和yourPassword替换为你的数据库用户名和密码。 4. 在Web应用程序中使用数据源:在你的Web应用程序的WEB-INF目录下的web.xml文件中,添加以下内容: ```xml <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/yourDBName</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> ``` 将上述代码中的yourDBName替换为你在context.xml文件中配置的数据源名称。 5. 在Java代码中使用数据源:在你的Java代码中,通过JNDI查找数据源并获取数据库连接。以下是一个简单的示例: ```java Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource dataSource = (DataSource) envContext.lookup("jdbc/yourDBName"); Connection connection = dataSource.getConnection(); ``` 将上述代码中的yourDBName替换为你在context.xml文件中配置的数据源名称。 这样,你就可以在Tomcat中成功连接MySQL数据库了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值