DOC

3046.docjava

By Josephine Hamilton,2014-10-12 14:20
9 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.