• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

原创:用JDBC访问AS/400数据- 配置与JDBC编程要点

发布: 2007-6-08 22:43 | 作者: seanhe | 来源: | 查看: 27次 | 进入软件测试论坛讨论

领测软件测试网
发了半天了,只见鱼过,不见冒泡,正在疑惑各位大大是否对此题目不感兴趣呢?luoshengzh大大的鼓励来了,谢谢luoshengzh。余文一并发出,不喜欢的就拍块砖吧,潜水的不要。 :em08: 

四.配置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.zip在I:\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();


附录A : 使用JDBC 驱动器连接AS/400 数据库

你可以使用DriverManager.getConnection() 方法来连接AS/400数据库. DriverManager.getConnection() 使用一个URL字符串作为参数. JDBC驱动器管理器将为尝试连接在URL字符串中所指的数据库:
   
"jdbc:as400://systemName/defaultSchema;listOfProperties"
以下是一些连接方式的例子

例一:URL不给出系统名。这种情况需要用户在使用时给出欲连接的系统名: 
      "jdbc:as400:"

 
例二:URL只给出系统名
      Connection c  = DriverManager.getConnection("jdbc:as400://mySystem");

 
例三:URL给出系统名,且给出缺省的Schema
      Connection c2 = DriverManager.getConnection("jdbc:as400://mySys2/mySchema");

例四:连接AS/400 数据库,且使用java.util.Properties 定义更多的JDBC 连接属性。
     // Create a properties object.
     Properties p = new Properties();
     // Set the properties for the connection.
     p.put("naming", "sql");
     p.put("errors", "full");
     // Connect using the properties object.
     Connection c = DriverManager.getConnection("jdbc:as400://mySystem",p);

例五:连接AS/400数据库,并且给出URL的相关属性. 
     // Connect using properties. The properties are set on the URL
     // instead of through a properties object.
     Connection c = DriverManager.getConnection( "jdbc:as400://mySystem;naming=sql;errors=full");

例六:连接AS/400数据库且给出用户名与口令
     // Connect using properties on the URL and specifying a user ID and password
     Connection c = DriverManager.getConnection(
                        "jdbc:as400://mySystem;naming=sql;errors=full",
                        "auser", "apassword");

例七:关闭数据库连接
 使用close() 方法将连接关闭,如 c.close();

 xuguopeng 回复于:2004-03-18 17:41:01
不错~~ 很棒~ 可惜看不懂~~ 没用过JDBC。。。。。。。

 moshco 回复于:2004-03-19 09:34:06
我是专门搜索你写的贴子了!写的很好!

 rollingpig 回复于:2004-03-19 13:35:05
加点注解
关于注册Driver部分
[quote:9653859d8b]
5.1 注册JDBC驱动器程序 
访问AS/400数据的JDBC驱动器程序叫com.ibm.as400.access.AS400JDBCDriver, 在JDBC编程中要建立程序与数据库的连接,首先得注册这个JDBC驱动器,请使用如下语句: 
java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ()); 
[/quote:9653859d8b]

通常,由于Driver类不一定已经存在于ClassPath里(可以在运行时才加上)
同时,Drvier类在初始化时会自动注册Driver
所以,这一段通常是这么写的
[code:1:9653859d8b]
try{
Class.forName("com.ibm.as400.access.AS400JDBCDriver"); 
}
catch(ClassNotFoundException e){
}


[/code:1:9653859d8b]
也可以达到同样效果,而且可以把DrvierName放在Properties里,随时改动Driver而不需重新改代码,如:

[code:1:9653859d8b]
String getProperties(String name){
.....//获取配置的属性
}

...
String driverClass = getProperties("driver");
String user=getProperties("user");
String pass=getProperties("pass");
String url=getProperties("url");

try{
Class.forName(driverClass); 
}
catch(ClassNotFoundException e){
}
Connection c = DriverManager.getConnection( ...);

[/code:1:9653859d8b]

 fh2001 回复于:2004-03-19 19:24:28
谢谢 rollingpig 大大补充。

 neosue 回复于:2004-03-20 10:19:39
請問如何才能得到 COLHDG 的內容?

 andrewleading_he 回复于:2004-03-22 14:36:49
呵呵,不錯啊,幫忙頂一下。不過聽說jdbc的效率比直接用ile rpg來操作數據要慢很多。尤其是add ,update動作!

 ecamel 回复于:2004-03-23 11:43:09
不错~!

 rufujian 回复于:2004-06-12 11:53:52
请问AS/400是不是必须安装AS/400 Toolbox for Java,如果我拿到jdbc驱动包
服务器上没有安装AS/400 Toolbox for Java,可不可以连接上

 fh2001 回复于:2004-06-12 22:39:46
[quote:385091cb76="rufujian"]请问AS/400是不是必须安装AS/400 Toolbox for Java,如果我拿到jdbc驱动包
服务器上没有安装AS/400 Toolbox for Java,可不可以连接上[/quote:385091cb76]
你可以试试看

 Amichael 回复于:2004-06-14 16:54:00
内容详细,谢谢了。

 mario663 回复于:2004-11-08 21:10:25
那里可以得到Jdbc驱动呀,急急!

 mario663 回复于:2004-11-08 21:50:11
我怎么找不到jt400.zip呀,我的Client Access 是英文版权所5.1,在安装显示AS/400 Toolbox for Java已安装,请那位高手指点,谢谢.

 bpcsusr 回复于:2004-11-09 09:15:55
得仔细看看.

 bobofish29 回复于:2004-11-09 09:52:20
[quote:8a8165d1f3="mario663"]我怎么找不到jt400.zip呀,我的Client Access 是英文版权所5.1,在安装显示AS/400 Toolbox for Java已安装,请那位高手指点,谢谢.[/quote:8a8165d1f3]
到目录"C:\Program Files\IBM\Client Access\jt400\lib\jt400.jar"中看一看,我的C/A是安装在C盘上的!

另外,请教楼主一问题,我用JAVA写了一个测试的小程序
代码如下:
[code:1:8a8165d1f3]
import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;
public class  CommandCall extends Object{
public static void main(String[] parmeters){
//Created a reader to get input from the user
BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
//Declare varialbes to hold the system name and the command to run
String systemString = null;
String commandString = null;

System.out.println("");
//Get the system name and the command to run from the user
try{
System.out.print("System name:");
systemString = inputStream.readLine();
}
catch (Exception e){};
System.out.println("");
//Create an as400 object.This is the system we send the command to
AS400 as400 = new AS400(systemString);
//Create a command call object specifying the server that will receive the command
CommandCall command = new CommandCall(as400);
//Run the command
try{
if (command.run(commandString))
System.out.print("Command successful");
else
System.out.print("Command failed");

//If messeage were produced from the command,print them
AS400Messeage[] messeageList = command.getMesseageList();
if (messeageList.length>0){
System.out.println(",messeage from the command:");
System.out.print("");
}
for (int i=0;i<messeageList.length ;i++ ){
System.out.print(messeageList[i].getID());
System.out.print(":");
System.out.print(messeageList[i].getText());
}
}
catch (Exception e){
System.out.println("Command" + command.getCommand() + "didn't run!");
}
System.exit(0);
}
}

[/code:1:8a8165d1f3]

以下是报错信息:
[code:1:8a8165d1f3]---------- javac ---------- 
CommandCall.java:23: cannot resolve symbol 
symbol  : constructor CommandCall (com.ibm.as400.access.AS400) 
location: class CommandCall 
      CommandCall command = new CommandCall(as400); 
                                      ^ 
CommandCall.java:26: cannot resolve symbol 
symbol  : method run (java.lang.String) 
location: class CommandCall 
         if (command.run(commandString)) 
                                   ^ 
CommandCall.java:32: cannot resolve symbol 
symbol  : class AS400Messeage 
location: class CommandCall 
      AS400Messeage[] messeageList = command.getMesseageList(); 
                ^ 
CommandCall.java:32: cannot resolve symbol 
symbol  : method getMesseageList () 
location: class CommandCall 
      AS400Messeage[] messeageList = command.getMesseageList(); 
                                                      ^ 
CommandCall.java:44: cannot resolve symbol 
symbol  : method getCommand () 
location: class CommandCall 
      System.out.println("Command" + command.getCommand() + "didn't run!"); 
                                                      ^ 
5 errors 

Output completed (5 sec consumed) - Normal Termination 
[/code:1:8a8165d1f3]

 just400 回复于:2004-11-09 10:06:34
ding....

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网