1.tomcat根目录下的lib中添加数据库连接的JAR包
2.修改tomcat根目录下的conf文件下的context.xml文件,修改内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for additional
information regarding copyright ownership. The ASF licenses this file to
You under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. --><!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve"
/> -->
<Resource name="jdbc/lcj(资源名称)" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://数据库物理地址/数据库名称"
username="数据库账号" password="数据库密码" maxActive="100" maxIdle="10" maxWait="-1"
removeAbandoned="true" removeAbandonedTimeout="20" logAbandoned="true" />
</Context>
3.修改项目的web.xml文件,在<web-app>标签之间添加以下代码
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/lcj(与上面的资源名称相匹配)</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.修改Spring配置文件,使Spring支持JNDI数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">
<!-- J2EE容器中使用JNDI数据源 -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/lcj(与上面的资源名称相匹配)" />
</beans>
就可以连接上数据库了 |