2012年6月21日15:21:59
程序代碼下載:
http://m.tkk7.com/Files/heyang/ViewDsSample.zip注意這個程序是RSA中的Dynamic Web Project

如果需要建立數據源,請參考:
http://m.tkk7.com/heyang/archive/2012/06/21/381257.html如果需要建立JDBC Provider,請參考:
http://m.tkk7.com/heyang/archive/2012/06/21/381254.html由于是示例代碼,所以比較簡單,功能就是訪問數據庫中一張表,然后把數據在表格中顯示出來。index.jsp中的代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" autoFlush="true" isThreadSafe="true" buffer="10kb" %>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.util.*,java.lang.*,java.math.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.ResultSet"%>
<html>
<head>
<title>View DataSource In Was</title>
</head>
<body>
<table width="100px" border="1">
<caption>Visit the data in DataSource</caption>
<thead>
<tr>
<td width="50px">Id</td>
<td width="50px">Name</td>
</tr>
</thead>
<tbody>
<%
Context ctx= new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/localdb2");// 注意這里需要和數據源配置中的jndi名對應上。
Connection con = ds.getConnection();
String sql = "select * from SYSTEM.TESTTB0619";
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
out.println("<tr>");
out.println("<td>"+id+"</td>");
out.println("<td>"+name+"</td>");
out.println("</tr>");
}
rs.close();
stmt.close();
con.close();
%>
</tbody>
</table>
</body>
</html>
首頁顯示效果如下: