新儒AS/400家园
  •  
       系统规划 | 系统配置 | 规划方案 | 其它 | 投 稿
         相关文章
    Home  首 页 »  其它 »  学习教程 » 

    用JDBC访问AS/400数据(二) - 配置、使用


    作者:fh2001 (fh2001@sina.com)   来源:原创    更新日期:2005-01-12    浏览次数:
     

     

    四.配置JDBC

    4.1       在配置JDBC之前,请确保工作站上的JDK已正确配置,且可以下常使用。

    4.2       找到JDBC软件包

    AS/400 Toolbox for Java安装后,用户访问AS/400数据的JDBC软件包即生成在IFS(集成文件系统)中,其路径是:/QIBM/ProdData/HTTP/Public/jt400/lib/ jt400.zip 。用户可以使用Client Access NetServer 将此路径MAP成一个本地磁盘驱动器,也可索性用FTP将其jt400.zip下载下来使用。

    4.3       设置环境路径(以jt400.zipI:\jt400\ 目录为例)

    4.3.1   Windows98 & 95环境,AUTOEXEC.BAT中增加一行:


    set classpath = %CLASSPATH%;I:\jt400\jt400.zip

     

     

    4.3.2  WindowsNT环境

    4.3.2.1        双击[我的电脑]图标

    4.3.2.2        双击[我的电脑]文件夹中的[控制面板]图标

    4.3.2.3        [控制面板]文件夹中双击[系统]图标

    4.3.2.4        选择[环境变量]面板

    4.3.2.5        增加CLASSPATH变量,再在下一行输入变量值,如下图:

     

     

     

     

    五.JDBC编程要点

    5.1 注册JDBC驱动器程序

                  访问AS/400数据的JDBC驱动器程序叫com.ibm.as400.access.AS400JDBCDriver JDBC编程中要建立程序与数据库的连接,首先得注册这个JDBC驱动器,请使用如下语句:

    java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());

     

    5.2 建立数据库连接

                  JDBC驱动器注册之后,第二步要做的就是建立数据库连接。可使用类似于如下语句的语句,更多的连接方式请见 附录A:

    Connection c = DriverManager.getConnection(

                            "jdbc:as400://mySystem;naming=sql;errors=full",

                            "auser", "apassword");

     

    5.3 使用SQL语句执行SQL操作

                  5.3.1 使用Statement接口

    Statement对象可用来执行一个简单的SQL语句,使用一个Connection对象创建一个Statement对象。如:c.createStatement()。具体使用如下例所示:

              // Connect to the AS/400.

           Connection c = DriverManager.getConnection("jdbc:as400://mySystem");

           // Create a Statement object.

           Statement s = c.createStatement();

           // Run an SQL statement that creates a table in the database.

           s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");

           // Run an SQL statement that inserts a record into the table.

           s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");

           // Run an SQL statement that inserts a record into the table.

           s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");

           // Run an SQL query on the table.

           ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");

           // Close the Statement and the Connection.

           s.close();

          c.close();

     

                  5.3.2 使用PreparedStatement接口

         PreparedStatement接口提供了一种灵活的SQL语句的执行方式。它可以在欲运行的SQL语句中预留下参数变量,在真正运行时将不用的参数调用则可获得不同的结果,这个接口在对数据进行批量同类执行时非常有用。请参见下面的例子:

              // Connect to the AS/400.

      Connection c = DriverManager.getConnection("jdbc:as400://mySystem");

          // Create the PreparedStatement object. It precompiles the specified SQL

      // statement. The question marks indicate where parameters must be set before

      // the statement is run.

     PreparedStatement ps = c.prepareStatement("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES (?, ?)");

              // Set parameters and run the statement.

                ps.setString(1, "JOSH");

          ps.setInt(2, 789);

          ps.executeUpdate();

              // Set parameters and run the statement again.

          ps.setString(1, "DAVE");

                ps.setInt(2, 456);

          ps.executeUpdate();

              // Close PreparedStatement and the Connection.

          ps.close();

                c.close();

     

                  5.3.3 使用CallableStatement接口

                    通过CallableStatement接口,可以运行SQL存储过程(stored procedures)。存储过程是随数据库保存的小程序。请参见下面的例子

     

           // Connect to the AS/400.

           Connection c = DriverManager.getConnection("jdbc:as400://mySystem");

           // Create the CallableStatement object. It precompiles the specified call to a stored

           // procedure. The question marks indicate where input parameters must be set and

       // where output parameters can be retrieved. The first two parameters are

           // input parameters, and the third parameter is an output parameter.

           CallableStatement cs = c.prepareCall("CALL MYLIBRARY.ADD (?, ?, ?)");

           // Set input parameters.

           cs.setInt (1, 123);

           cs.setInt (2, 234);

           // Register the type of the output parameter.

           cs.registerOutParameter (3, Types.INTEGER);

           // Run the stored procedure.

           cs.execute ();

           // Get the value of the output parameter.

           int sum = cs.getInt (3);

           // Close the CallableStatement and the Connection.

           cs.close();

           c.close();

     

                  5.3.4 使用ResultSet接口

    使用ResultSet接口可以得到SQL语句所获得的数据结果集,并且可以使用游标 Cursor)来方便地浏览数据结果集。请参见以下的例子:

     

           // Connect to the AS/400.

           Connection c = DriverManager.getConnection("jdbc:as400://mySystem");

           // Create a Statement object.  Set the result set

                  // type to scroll insensitive.

           Statement s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

                                     ResultSet.CONCUR_READ_ONLY);

           // Run a query. The result is placed in a ResultSet object.

           ResultSet rs = s.executeQuery ("SELECT NAME,ID FROM

               MYLIBRARY.MYTABLE");

           // Iterate through the rows of the ResultSet.Go to the next row if the user selected 'N'

       // or previous record if the user selected 'P'.  Assume that getUserSelection() is

       // defined elsewhere.

           boolean done = false;

           while (! done)

           {

                     switch (getUserSelection ())

                     {

                     case 'N':

                          rs.next ();

                          break;

                     case 'P':

                          rs.previous ();

                          break;

                     default:

                          done = true;

                      }

            // Get the values from the ResultSet. The first value is a string, and

            // the second value is an integer.

            String name = rs.getString("NAME");

            int id = rs.getInt("ID");

            System.out.println("Name = " + name);

            System.out.println("ID = " + id);

            }

            // Close the Statement and the Connection.

            s.close();

            c.close();

     

    (责任编辑:artmanager)
    [推荐给朋友] [显示打印版本]

      相关文章
      相关评论

    您的姓名:
    评论内容:
     
    设为首页 | 加入收藏 | 联系我们 | 友情链接 | 管理登录
    copyright by 新儒AS/400家园 站长:山人
    Power by 9466Aricle