1.配置连接数据源参数
方法a:
在${tomcatPath}/conf/context.xml 中改成以入内容:
==============================================================
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/oraclePool" auth="Container"
type="javax.sql.DataSource" username="orca" password="orca"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.100:1521:TAINK" maxActive="100"
maxIdle="30" maxWait="1000" />
</Context>
==============================================================
方法b:
在项目中的WebRoot/META-INF/目录下面添加一个context.xml 文件 ,内容如下:
==============================================================
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/oraclePool" auth="Container"
type="javax.sql.DataSource" username="orca" password="orca"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.100:1521:TAINK" maxActive="100"
maxIdle="30" maxWait="1000" />
</Context>
==============================================================
2.修改WebRoot/WEB-INF/web.xml 文件,加入以下内容
==============================================================
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/mysqlPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
==============================================================
3. 在${tomcatPath}/lib/目录下添加 连接数库的jar 包
4. 在dao 中调用数据源 code 如下:
==============================================================
package org.taink.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.taink.entity.UserBean;
public class UserBeanDao {
private DataSource dataSource = null;
public UserBeanDao() {
if (dataSource == null) {
try {
Context context = new InitialContext();
dataSource = (DataSource) context
.lookup("java:comp/env/jdbc/oraclePool");
} catch (NamingException e) {
System.out
.println("++++++++++++ loading datasource error +++++++++++");
e.printStackTrace();
}
}
}
public List getUserList() {
List userList = null;
try {
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_account");
ResultSet rs = ps.executeQuery();
userList = new ArrayList();
while (rs.next()) {
UserBean userBean = new UserBean();
userBean.setUserId(rs.getInt(1));
userBean.setUserName(rs.getString(2));
userBean.setPassword(rs.getString(3));
userBean.setGroupName(rs.getString(4));
userList.add(userBean);
}
} catch (SQLException e) {
System.out.println("++++++++++++ connection db error +++++++++++");
e.printStackTrace();
}
return userList;
}
}
==============================================================
5.编写测试页面
==============================================================
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="org.taink.dao.UserBeanDao"%>
<%@page import="java.util.List"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>result page</title>
<%
UserBeanDao userDao = new UserBeanDao();
List resultList = userDao.getUserList();
request.setAttribute("list", resultList);
%>
</head>
<body>
<br />
<c:forEach items="${list}" var="item">
userId:<c:out value="${item.userId}"></c:out>
username: <c:out value="${item.userName}"></c:out>
password: <c:out value="${item.password}"></c:out>
groupname: <c:out value="${item.groupName}"></c:out>
<hr />
</c:forEach>
</body>
</html:html>
==============================================================