DOC

3046.docjava

By Josephine Hamilton,2014-10-12 14:20
8 views 0
3046.docjava

    由于程序的原因,本文件未被完整保存。

    首页 ? 研究文集 ? 开发框架

    Java Workshop--(3) JSP flyingwcy 原创 (参与分!288296,专家分!3745) 发表!2003-11-24 14:57 更新!2003-11-25

     08:37 版本!1.0 阅读!2181

    上一期PreparedStatement的答案如下

    1. import java.sql.*;

    2.

    3. class JPC_JDBCSample2 {

    4. public static void main(String args[]) { 5. Connection con = null;

    6. PreparedStatement pstmt = null;

    7. String sql = null;

    8. ResultSet rs = null;

    9. String div = null; // Division

    10. short total= 0; // Total amount of managers 11.

    12. // Set a String input in command line as in the division variable

    13. if(args.length >0){

    14. div = args[0]; 15. }else{

    16. System.out.println("Please input a division"); 17. return;

    18. }

    19.

    20. try {

    21. // Load the driver

    22. Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); 23.

    24. // Set JDBC URL in a variable

    25. // The URL is jdbc:db2:dbname

    26. String url = "jdbc:db2:sample";

    27.

    28. // Connect to the DB specified by the JDBC URL 29. // with the user ID (db2admin) and the password (passwor d) 30. con = DriverManager.getConnection(url,"db2admin","password");

    31.

    32. // +++(1) Specify a SQL statement

    33. sql = "SELECT DEPTNAME,MANAGER FROM org WHERE DIVISION =?"; 34.

    35. // +++(2) Create a PreparedStatement object

    36. pstmt = con.prepareStatement(sql); 37.

    38. // +++(3) Set a parameter in the PreparedStatement object

    39. pstmt.setString(1, div);

    40.

    41. // +++(4) Execute a query

    42. rs = pstmt.executeQuery(); 43.

    44. // +++(5)(6)Retrieve the result from the ResultSet

    45. // +++ object and display it

    46. System.out.println("***** " + div + " *****");

    47. while (rs.next()) {

    48. String dname = rs.getString(1); 49. short manager = rs.getShort(2); 50. System.out.print( dname+": "); 51. System.out.println( manager); 52. total += manager;

    53. }

    54. System.out.println("Total: "+total); 55.

    56. } catch (ClassNotFoundException e){ 57. e.printStackTrace();

    58. } catch (SQLException e) {

    59. e.printStackTrace();

    60. }finally{

    61. try{

    62. // Release resources

    63. rs.close();

    64. pstmt.close();

    65. con.close();

    66. }catch(SQLException e){

    67. e.printStackTrace();

    68. }

    69. }

    70. }

    71.}

    这里给出了一个javabean的代码,然后请试着给出jspjavabean交互的代码

    1. package jpc;

    2. /*

    3. * AddBean.java

4. *

    5. */

    6. public class AddBean {

    7.

    8. private int data1;

    9. private int data2;

    10. /**

    11. *

    12. * Constructor

    13. */

    14. public AddBean () {

    15. }

    16. /**

    17. * Return the value of the Data1 as a String.

    18. */

    19. public String getData1() { 20. return String.valueOf(data1); 21. }

    22. /**

    23. * Set Data1

    24. */

    25. public void setData1(String str) { 26. try {

    27. this.data1 = Integer.parseInt(str);

    28. }

    29. catch (NumberFormatException e) { 30. this.data1 = 0;

    31. }

    32. }

    33. /**

    34. * Return the value of the Data2 as a String.

    35. */

    36. public String getData2() { 37. return String.valueOf(data2); 38. }

    39. /**

    40. * Set Data2

    41. */

    42. public void setData2(String str) { 43. try {

    44. this.data2 = Integer.parseInt(str);

    45. }

    46. catch (NumberFormatException e) { 47. this.data2 = 0;

48. }

    49. }

    50. /**

    51. * Return the result of the addition as a String. 52. */

    53. public String getResult() {

    54. return String.valueOf(data1 + data2); 55. }

    56.}

    以下是调用javabeanjsp代码框架。

    1. <html>

    2. <head>

    3. <title> Exercise JSP </title>

    4. </head>

    5.

    6. <body>

    7. <h1> Exercise JSP </h1>

    8.

    9. <hr>

    10.

    11.<!-- +++ (1) Declare a Bean -->

    12.[ 1 ]

    13.

    14.<form action="/JPC/jsp/Add.jsp" method=POST> 15. DATA1 <input type=text name=param1 ><br> 16. DATA2 <input type=text name=param2 ><br> 17.<input type=submit value="Submit">

    18.</form>

    19.

    20.<!-- +++ (2) Get the value of param1 and Set it to the data1 property of the Bean -->

    21.[ 2 ]

    22.<!-- +++ (3) Get the value of param2 and Set it to the data2 property of the Bean -->

    23.[ 3 ]

    24.

    25.<h2>

    26.<!-- +++ (4) Get data1 from the property of the Bean -->

    27.[ 4 ]

    28.<!-- +++ (5) Get data2 from the property of the Bean -->

    29.[ 5 ]

    30.

    31.</h2>

    32.

    33.</body>

    34.</html>

版权声明 给作者写信 本篇文章对您是否有帮助, 投票! 投票结果!

    作者其它文章!

    ; JAVA规则 开发篇

    ; Servlet设计

    ; IBM WSAD JAVA工具实验

    ; XML and Database Tooling(中英文对照版本)

    ; IBM WSAD 调试应用程序

    作者全部文章 上一篇文章 下一篇文章 返回〔开发框架〕

Report this document

For any questions or suggestions please email
cust-service@docsford.com