先看代码!各个属性呢都应该看得懂
<Context>
<Resource name="jdbc/test"
type="javax.sql.DataSource"
username="liuyong"
password="liuyong"
driverClassName="oracle.jdbc.OracleDriver"
maxIdle="2"
maxWait="5000"
url="jdbc:oracle:thin:@localhost:1521:liuyong"
maxActive="4"/>
</Context>
一:把上面的代码,加入tomcat,安装目录下conf文件夹下的server.xml中,位置放在 </GlobalNamingResources>节点之后吧!(E:\tomcat6\conf\)
二:另外再建一个和你工程名一样的.xml文件,把上述代码COPY进去,然后保存放在E:\tomcat6\conf\Catalina\localhost文件夹中,如果没有Catalina目录就自己建,好
三:之后呢在你的工程web.xml中加入如下代码:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource </res-type>
<res-auth>Container</res-auth>
</resource-ref>
完了,配置结束,开始测试:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>tomcat datasource test</title>
</head>
<body>
<%
Context initContext = new InitialContext();
//法一:java:/comp/env这个玩意是固定的
//Context envContext = (Context) initContext.lookup("java:/comp/env");
//DataSource db = (DataSource)envContext.lookup("jdbc/test");
//法二:
DataSource db = (DataSource)initContext.lookup("java:/comp/env/jdbc/test");
Connection conn = db.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM XIAO");
while(rs.next()){
out.print(rs.getString("name")+"__ ");
out.print(rs.getString("school")+"<br>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
game over
周末真无聊啊!