免费预览已结束,剩余13页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AS400上面开发java技术调查方案 做成者:段高辉 日期:2012-3-281. 调查目的在AS400上使用RPG开发,操作数据的效率当然会快很多,但是RPG处理数据逻辑毕竟是非常麻烦。其实在AS400 v5r4以上的版本都是可以支持java开发,下面是一个用Java开发通过JDBC调用ORACLE数据库的功能。2. 环境准备2.1创建java工作目录:如图:输入CL命令:MD DIR (javaPgm)查看java工作目录:如图:输入CL命令:QSH ,进入上图,打 ls 命令,出现2.1创建的java工作目录(备注:删除该目录直接使用:RMDIR javaPgm创建源文件CRTSRCPFFILE(LIBNAME/FILENAME)IGCDTA(*YES)创建java文件:WRKOBJPDM LIB(ZTEST) OBJ(QJAVASRC) 然后输入 122.5在当前画面按下F6毽进入创建源代码界面: /* * * 方法名 : readData * 描述 : 读取数据库操作 * param sql * return * * ADD BY DUAN GAOHUI AT TIME 2012-1-16 */public String readData(String sql)Connection conn = null;Statement stmt = null;ResultSet rs = null;String name = ;conn = getConnection();try stmt = conn.createStatement();rs = stmt.executeQuery(sql);while(rs.next() name = rs.getString(NAME);rs.close();stmt.close(); catch (SQLException e) System.out.println(ERROR: 读数据失败); finally closeConnection(conn);return name;配置环境变量 New value . . . . . . . . . . . JavaPgm:/qibm/ProdData/OS400/Java400/ojdbc14.jar:/javaPgm/*.class 如果不存在jar包,请将jar文件放到指定路径下面备注:如果之前已经设置过变量“CLASSPATH,可在此变量前输入”2“进入修改,否则需要新增”CLASSPATH”变量,注意:如果有多个路径中间使用“:”分隔26将源代码复制成Stream File CPYTOSTMF FROMMBR(/qsys.lib/studgh.lib/qjavasrc.file/DBConn.mbr) TOSTMF(javaPgm/DBConn.java) STMFOPT(*REPLACE) STMFCODPAG(*PCASCII)按F4进入下面画面:执行确定。编译java文件用CL命令:QSH 进入编译两个文件(两个文件的代码,见附录)成功后开始编写RPG程序3. 开始编写RPG程序 执行结果:辅助资料:Examples of Prototyping Java MethodsThis section presents some examples of prototyping Java methods. Example 1The Java Integer class contains a static method called toString, which accepts an int parameter, and returns a String object. It is declared in Java as follows: static String Integer.toString(int)This method would be prototyped as follows: D tostring PR O EXTPROC(*JAVA:D java.lang.Integer:D toString)D CLASS(*JAVA:java.lang.String)D STATICD num 10I 0 VALUE The EXTPROC keyword identifies the method as a Java method. It also indicates that the method name is toString, and that it is found in class java.lang.Integer. The O in column 40 and the CLASS keyword tell the compiler that the method returns an object, and the class of that object is java.lang.String. The STATIC keyword indicates that the method is a static method, meaning that an Integer object is not required to call the method. The data type of the parameter is specified as 10I, which maps to the Java int data type. Because the parameter is an int, it must be passed by value, and the VALUE keyword is required. Example 2The Java Integer class contains a static method called getInteger, which accepts String and Integer objects as parameters, and returns an Integer object. It is declared in Java as follows: static Integer Integer.getInteger(String, Integer)This method would be prototyped as follows: D getint PR O EXTPROC(*JAVA:D java.lang.Integer:D getInteger)D CLASS(*JAVA:java.lang.Integer)D STATICD string O CLASS(*JAVA:java.lang.String) CONSTD num O CLASS(*JAVA:java.lang.Integer) CONST This method accepts two objects as parameters. O is coded in column 40 of the D-specification and the CLASS keyword specifies the class of each object parameter. Because both parameters are input-only, the CONST keyword is specified. Example 3The Java Integer class contains a method called shortValue, which returns the short representation of the Integer object used to invoke the method. It is declared in Java as follows: short shortValue()This method would be prototyped as follows: D shortval PR 5I 0 EXTPROC(*JAVA:D java.lang.Integer:D shortValueThe STATIC keyword is not specified because the method is not a static method. The method takes no parameters, so none are coded. When you call this method, you will specify the Integer instance as the first parameter. The returned value is specified as 5I, which maps to the Java short data type. Example 4The Java Integer class contains a method called equals, which accepts an Object as parameter and returns a boolean. It is declared in Java as follows: boolean equals(Object)This method would be prototyped as follows: D equals PR N EXTPROC(*JAVA:D java.lang.Integer:D equals)D obj O CLASS(*JAVA:java.lang.Object) The returned value is specified as N, which maps to the Java boolean data type. Because this is not a static method, a call to this method will have two parameters with the instance parameter coded first. WebSphere Development Studio: ILE RPG Programmers GuideCalling Java Methods from ILE RPG This section describes how to call Java methods from ILE RPG programs. If the method is not a static method, then it is called an instance method and an object instance must be coded as an extra first parameter in order to call the method. For example, if an instance method is prototyped with one parameter, you must call it with two parameters, the first being the instance parameter. The following steps describe the call from ILE RPG to a Java method: 1.Java methods can be called using existing operation codes CALLP (when no return value is expected) and EVAL (when a return value is expected). When your RPG procedure attempts to make call to a Java method, RPG will check to see if the Java Virtual Machine (JVM) has been started. If not, RPG will start the JVM for you. It is also possible to start JVM yourself using the JNI function described in Creating the Java Virtual Machine (JVM) 2.If you are using your own classes (or any classes outside the normal java.xxx classes), be sure to have your CLASSPATH environment variable setup before you call any Java methods. When RPG starts up the JVM for you, it will add the classes in your CLASSPATH environment variable to the standard classpath, so when you use your own classes, Java will be able to find them. Set the CLASSPATH environment variable interactively like this: =ADDENVVAR ENVVAR(CLASSPATH) VALUE(/myclasses/:/xyzJava/classes/)The directories must be separated by colons. 3.Normally, Java does its own garbage collection, detecting when an object is no longer needed. When you create objects by calling Java constructors from your non-native RPG procedure, Java has no way of knowing that the object can be destroyed, so it never destroys them. You can enable garbage collection for several objects at once by calling the JNI functions described in Telling Java to free several objects at once . If you know you are not going to need an object any more, you should tell this to Java by calling the JNI function described in Telling Java you are finished with a temporary object . Caution:Since Java uses threads, the THREAD(*SERIALIZE) keyword must be coded in all modules that interact with Java. RPG relies heavily on static storage even in subprocedures that apparently only use automatic storage. THREAD(*SERIALIZE) is necessary to ensure the correct handling of this static storage. This applies not only to modules that contain calls to Java methods, but also to any modules that might be called during interactions with Java. See Additional RPG Coding for Using Java for more information about the various JNI functions. Example 1 In this example, the goal is to add two BigDecimal values together. In order to do this, two BigDecimal objects must be instantiated by calling the constructor for the BigDecimal class, fields must be declared to store the BigDecimal objects, and the add() method in the BigDecimal class must be called. Figure 77. RPG Code Example Calling BigDecimal Java Class * Prototype the BigDecimal constructor that accepts a String * parameter. It returns a new BigDecimal object. * Since the string parameter is not changed by the constructor, we will * code the CONST keyword. This will make it more convenient * to call the constructor. *D bdcreate1 PR O EXTPROC(*JAVA:D java.math.BigDecimal:D *CONSTRUCTOR)D str O CLASS(*JAVA:java.lang.String)D CONST * * Prototype the BigDecimal constructor that accepts a double * parameter. 8F maps to the Java double data type and so must * be passed by VALUE. It returns a BigDecimal object. *D bdcreate2 PR O EXTPROC(*JAVA:D java.math.BigDecimal:D *CONSTRUCTOR)D double 8F VALUE * Define fields to store the BigDecimal objects. *D bdnum1 S O CLASS(*JAVA:java.math.BigDecimal)D bdnum2 S O CLASS(*JAVA:java.math.BigDecimal) * * Since one of the constructors we are using requires a String object, * we will also need to construct one of those. Prototype the String * constructor that accepts a byte array as a parameter. It returns * a String object. *D makestring PR O EXTPROC(*JAVA:D java.lang.String:D *CONSTRUCTOR)D bytes 30A CONST VARYING * * Define a field to store the String object. *D string S O CLASS(*JAVA:java.lang.String) * * Prototype the BigDecimal add method. It accepts a BigDecimal object * as a parameter, and returns a BigDecimal object (the sum of the parameter * and of the BigDecimal object used to make the call). *D add PR O EXTPROC(*JAVA:D java.math.BigDecimal:D add)D CLASS(*JAVA:java.math.BigDecimal)D bd1 O CLASS(*JAVA:java.math.BigDecimal) D CONST * * Define a field to store the sum. *D sum S O CLASS(*JAVA:java.math.BigDecimal)DD double S 8F INZ(1.1)D fld1 S 10A * Define a prototype to retrieve the String version of the BigDecimalD getBdString PR O CLASS(*JAVA:java.lang.String)D EXTPROC(*JAVA:D java.lang.BigDecimal:D toString) * Define a prototype to retrieve the value of a StringD getBytes PR 65535A VARYINGD EXTPROC(*JAVA:D java.lang.String:D getBytes) * Define a variable to hold the value of a BigDecimal objectD bdVal S 63P 5Here is the code that does the call. Figure 78. * Call the constructor for the String class, to create a String * object from fld1. Since we are calling the constructor, we * do not need to pass a String object as the first parameter. *C EVAL string = makestring(123456789012345678901234567890) * * Call the BigDecimal constructor that accepts a String * parameter, using the String object we just instantiated. *C EVAL bdnum1 = bdcreate1(string) * * Call the BigDecimal constructor that accepts a double * as a parameter. *C EVAL bdnum2 = bdcreate2(double) * * Add the two BigDecimal objects together by calling the * add method. The prototype indicates that add accepts * one parameter, but since add is not a static method, we * must also pass a BigDecimal object in order to make the * call, and it must be passed as the first parameter. * bdnum1 is the object we are using to make the * call, and bdnum2 is the parameter. *C EVAL sum = add(bdnum1:bdnum2) * sum now contains a BigDecimal object with the value * bdnum1 + bdnum2.C EVAL bdVal = %DECH(getBdString(sum) : 63 : 5) * val now contains a value of the sum. * If the value of the sum is larger than the variable val can * hold, an overflow exception would occur.Example 2 This example shows how to perform a TRIM in Java by using the trim() method as an alternative to the ILE RPG %TRIM built-in function. The trim() method in the String class is not a static method, so a String object is needed in order to call it. Figure 79. RPG Code Example Using trim() Java Method * Define a field to store the String object we wish to trim *D str S O CLASS(*JAVA:java.lang.String) * * Prototype the constructor for the String class. The * constructor expects a byte array. *D makestring PR O EXTPROC(*JAVA:D java.lang.String:D *CONSTRUCTOR)D CLASS(*JAVA:java.lang.String)D parm 10A * * Prototype the String method getBytes which converts a String to a byte * array. We can then store this byte array in an alpha field. *D getBytes PR 10A EXTPROC(*JAVA:D java.lang.String:D getBytes) VARYING * * Prototype the String method trim. It doesnt take any parameters, * but since it is not a static method, must be called using a String * object. *D trimstring PR O EXTPROC(*JAVA:D java.lang.String:D trim)D fld S 10A INZ( hello ) VARYINGThe call is coded as follows: Figure 80. RPG Call to the String constructor * Call the String constructor *C EVAL str = makestring(fld) * * Trim the string by calling the String trim() method. * We will reuse the str field to store the result. *C EVAL str = trimstring(str) * * Convert the string back to a byte array and store it * in fld. *C EVAL fld = getBytes(str)Static methods are called
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026湖南省机关事业单位工勤技能岗位考试(收银员·高级)历年参考题库含答案详解
- 某化工企业员工激励规范
- 实施工作失误反思改正方案
- 2026年中卫市制冷与空调设备安装修理作业证考试练习试卷(含答案)
- 2026年重庆市荣昌区观光车和观光列车司机 N2 证考试练习试卷(含答案)
- 2026年真空钎焊机组真空度检测理论考试练习试卷(含答案)
- 电工年审考试复习资料
- 国家物业前期服务合同范本
- 2026年icu护理三基考试题库及答案详解
- 2026年护师考试模拟题题库及答案详解
- 月球基地通信网络构建-全面剖析
- 数学拓展社团课件
- 设备管理岗位竞聘
- 人教版小学五年级上册《信息科技》全套完整版课件
- 石墨调控(im)SiC-Cu复合材料工艺及性能研究
- 学校传染病和突发公共卫生事件处理流程图
- 施工现场交通安全培训
- 家庭成员及主要社会关系情况表
- 小型贸易公司员工手册
- 财务报表分析财务报表分析财务报表分析财务报表
- 大型发电厂和变电站接地网状态评估报告
评论
0/150
提交评论