步骤一、META-INF下添加context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/oracle" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="5000" username="test"
password="test" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
</Context>
步骤二、WEB-INF下web.xml中添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
步骤三、在index.jsp
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*" %>
<body>中添加如下代码:
<%
ResultSet rSet=null;
PreparedStatement ps=null;
Connection conn=null;
String sqlString=null;
try {
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
conn=ds.getConnection();
sqlString="select a,b from testa";
ps=conn.prepareStatement(sqlString);
rSet=ps.executeQuery();
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+" 学号");
out.print("<TH width=100>"+" 姓名");
out.print("</TR>");
while(rSet.next())
{
out.print("<TR>");
out.print("<TD >"+rSet.getString(1)+"</TD>");
out.print("<TD >"+rSet.getString(2)+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
} catch (Exception e) {
e.printStackTrace();
}finally{
ps.close();
rSet.close();
conn.close();
}
%>
步骤四、oracle数据库中建表
create table TESTA
(
A VARCHAR2(10),
B VARCHAR2(50)
);
本文介绍如何通过在META-INF下添加context.xml文件配置Oracle数据库连接池,并在WEB-INF下的web.xml中引用,最后展示了如何在index.jsp页面使用JNDI获取连接池资源并查询数据。
852

被折叠的 条评论
为什么被折叠?



