中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > Struts/Hibernate
Java Workshop--(3) JSP
作者:未知 时间:2005-07-24 21:22 出处:JR 责编:chinaitpower
              摘要:Java Workshop--(3) JSP
上一期PreparedStatement的答案如下
  1. import java.sql.*;
  2. class JPC_JDBCSample2 {
  3.  public static void main(String args[]) {
  4.   Connection con = null;
  5.   PreparedStatement pstmt = null;
  6.   String sql = null;
  7.   ResultSet rs = null;
  8.   String div = null// Division
  9.   short total= 0; // Total amount of managers
  10.   
  11.   // Set a String input in command line as in the division variable
  12.   if(args.length >0){
  13.    div = args[0];
  14.   }else{
  15.    System.out.println("Please input a division");
  16.    return;
  17.   }
  18.   
  19.   try {
  20.    // Load the driver
  21.    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
  22.    // Set JDBC URL in a variable
  23.    // The URL is jdbc:db2:dbname
  24.    String url = "jdbc:db2:sample";
  25.    
  26.    // Connect to the DB specified by the JDBC URL
  27.    // with the user ID (db2admin) and the password (password)
  28.    con = DriverManager.getConnection(url,"db2admin","password");
  29.    // +++(1) Specify a SQL statement
  30.    sql = "SELECT DEPTNAME,MANAGER FROM org WHERE DIVISION =?";
  31.    
  32.    // +++(2) Create a PreparedStatement object
  33.    pstmt = con.prepareStatement(sql);
  34.    
  35.    // +++(3) Set a parameter in the PreparedStatement object
  36.    pstmt.setString(1, div);
  37.    // +++(4) Execute a query
  38.    rs = pstmt.executeQuery();
  39.    
  40.    // +++(5)(6)Retrieve the result from the ResultSet 
  41.                // +++      object and display it
  42.    System.out.println("***** " + div + " *****");
  43.    while (rs.next()) {
  44.     String dname  = rs.getString(1);
  45.     short manager = rs.getShort(2);
  46.     System.out.print( dname+": ");
  47.     System.out.println( manager);
  48.     total += manager;
  49.    }
  50.    System.out.println("Total: "+total);
  51.    
  52.   } catch (ClassNotFoundException e){
  53.    e.printStackTrace();
  54.   } catch (SQLException e) {
  55.    e.printStackTrace();
  56.   }finally{
  57.    try{
  58.     // Release resources
  59.     rs.close();
  60.     pstmt.close();
  61.     con.close();
  62.    }catch(SQLException e){
  63.     e.printStackTrace();
  64.    }
  65.   }
  66.  }
  67. }


这里给出了一个javabean的代码,然后请试着给出jsp和javabean交互的代码
  1. package jpc;
  2. /*
  3. * AddBean.java
  4. */
  5. public class AddBean {
  6.     private int data1;
  7.     private int data2;
  8.     /**
  9.     * 
  10.     * Constructor
  11.     */
  12.     public AddBean () {
  13.     }
  14.     /**
  15.     * Return the value of the Data1 as a String.
  16.     */
  17.     public String getData1() {
  18.         return String.valueOf(data1);
  19.     }
  20.     /**
  21.     * Set Data1
  22.     */
  23.     public void setData1(String str) {
  24.         try {
  25.              this.data1 = Integer.parseInt(str);
  26.         }
  27.         catch (NumberFormatException e) {
  28.             this.data1 = 0;
  29.         }
  30.     }
  31.     /**
  32.     * Return the value of the Data2 as a String.
  33.     */
  34.     public String getData2() {
  35.         return String.valueOf(data2);
  36.     }
  37.     /**
  38.     * Set Data2
  39.     */
  40.     public void setData2(String str) {
  41.         try {
  42.             this.data2 = Integer.parseInt(str);
  43.         }
  44.         catch (NumberFormatException e) {
  45.             this.data2 = 0;
  46.         }
  47.     }
  48.     /**
  49.     * Return the result of the addition as a String.
  50.     */
  51.     public String   getResult() {
  52.         return String.valueOf(data1 + data2);
  53.     }



以下是调用javabean的jsp代码框架。
  1. <html>
  2. <head>
  3.    <title> Exercise JSP </title>
  4. </head>
  5. <body>
  6. <h1> Exercise JSP </h1>
  7. <hr>
  8. <!-- +++ (1) Declare a Bean -->
  9. [ 1 ]
  10. <form action="/JPC/jsp/Add.jsp" method=POST>
  11.   DATA1 <input type=text name=param1 ><br>
  12.   DATA2 <input type=text name=param2 ><br>
  13. <input type=submit value="Submit">
  14. </form>
  15. <!-- +++ (2) Get the value of param1 and Set it to the data1 property of the Bean -->
  16. [ 2 ]
  17. <!-- +++ (3) Get the value of param2 and Set it to the data2 property of the Bean -->
  18. [ 3 ]
  19. <h2>
  20. <!-- +++ (4) Get data1 from the property of the Bean -->
  21. [ 4 ] 
  22. <!-- +++ (5) Get data2 from the property of the Bean -->
  23. [ 5 ] 
  24. </h2>
  25. </body>
  26. </html>

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有