<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/usettest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>(注意:<res-ref-name>jdbc/usettest</res-ref-name>
中的usettest一定要和数据库的名称一样。)
6.书写index.jsp文件:
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,javax.naming.*"%><% Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/usettest");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(strSql);%><body>
<%while(rs.next()){%>
<p>
<%=rs.getString("username")%>
</p>
<%}%>
(注意:Object obj = (Object) ctx.lookup("jdbc/usettest");
这里也要和数据库的名称一样。)