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

          真正的快樂來源于創(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
          注意這個(gè)程序是RSA中的Dynamic Web Project


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

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

          由于是示例代碼,所以比較簡(jiǎn)單,功能就是訪問數(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名對(duì)應(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 何楊 閱讀(373) 評(píng)論(0)  編輯  收藏 所屬分類: RSA
          主站蜘蛛池模板: 西和县| 崇州市| 射洪县| 瓦房店市| 清镇市| 绩溪县| 和田市| 都江堰市| 六安市| 高唐县| 长宁县| 延吉市| 塔河县| 大新县| 北安市| 石门县| 礼泉县| 双辽市| 上高县| 永城市| 凤山县| 大埔县| 聊城市| 西乌珠穆沁旗| 繁峙县| 同仁县| 略阳县| 黔江区| 泰宁县| 保定市| 六枝特区| 舒城县| 昌吉市| 长岭县| 惠来县| 贵阳市| 和静县| 阿合奇县| 调兵山市| 集安市| 五常市|