當(dāng)柳上原的風(fēng)吹向天際的時候...

          真正的快樂來源于創(chuàng)造

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          2012年6月21日15:21:59

          程序代碼下載:
          http://www.aygfsteel.com/Files/heyang/ViewDsSample.zip
          注意這個程序是RSA中的Dynamic Web Project


          如果需要建立數(shù)據(jù)源,請參考:
          http://www.aygfsteel.com/heyang/archive/2012/06/21/381257.html

          如果需要建立JDBC Provider,請參考:
          http://www.aygfsteel.com/heyang/archive/2012/06/21/381254.html

          由于是示例代碼,所以比較簡單,功能就是訪問數(shù)據(jù)庫中一張表,然后把數(shù)據(jù)在表格中顯示出來。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");// 注意這里需要和數(shù)據(jù)源配置中的jndi名對應(yīng)上。
                      
                      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>

          首頁顯示效果如下:
          posted on 2012-06-21 15:25 何楊 閱讀(362) 評論(0)  編輯  收藏 所屬分類: RSA
          主站蜘蛛池模板: 松桃| 许昌县| 阿巴嘎旗| 蒲江县| 梅州市| 清远市| 东台市| 安岳县| 新宾| 云阳县| 恩平市| 宜兰县| 长治县| 江都市| 铅山县| 奉贤区| 宁都县| 清水河县| 嘉定区| 娄烦县| 呼伦贝尔市| 奈曼旗| 景宁| 开平市| 承德县| 古浪县| 丰顺县| 鸡泽县| 黄平县| 奉化市| 喜德县| 赤峰市| 四川省| 崇礼县| 淄博市| 夏津县| 攀枝花市| 台北县| 中西区| 剑阁县| 特克斯县|