1.打开Tomcat6.0、conf/context.xml文件,编辑成如下格式:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/myOracle"
type="javax.sql.DataSource"
driverClassName="scott" password="tiger" maxActive="200" maxIdle="10" maxWait="50"/>
</Context>
2.然后再web.xml文件中的web-app节点下加入如下代码形式:
<resource-ref>
<description>myOracle connection oracle9i</description>
<res-ref-name>jdbc/orcl</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
3.新建一个JSP文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
try{
Context envContext=(Context)new InitialContext().lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/myOracle");
Connection conn=ds.getConnection();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select ename from emp");
while(rs.next()){
out.println(rs.getString("ename")+"<br>");
}
}catch(NamingException e){
e.printStackTrace();
}
%>
</body>
</html>