java调用rpg完整例子,仅供参考!
由于需要在网上找了完整的例子,希望对有需要的朋友有所帮助,原文如下:
Accessing RPG from JavaServer Pages
by Don Denoncourt
Simple encapsulation makes it easy to integrate JavaServer Pages with RPG.
Published September 2002
Now shipping from MCPress!
Java Application Strategies for iSeries and AS/400
Don't just say you know Java. Know how to make it sing.
For this article, I was given the task of writing a JavaServer Page (JSP) that accesses an RPG program. The RPG program, shown in Figure 1, takes input parameters (a character, a decimal, and a date) and returns output parameters (a character, a decimal, a date, and an array of dates). The JSP code required to invoke the RPG, as you can see in Figure 2, is trivial. Sample output from the JSP example is shown in Figure 3. The code for the JSP is trivial because the complexity of accessing RPG from Java is encapsulated in a JavaBean, shown in Figure 4. All these figures are shown directly below.
*--------------------------------------------------------------
* Simple RPG program to act as stored procedure.
*--------------------------------------------------------------
* Input parameters:
*
* inChar - a 10 character field
* inDecimal - a 10 digit, 0 decimal place field
* inDate - a date
*
* Output parameters:
*
* outchar - inChar translated to all lowercase alpha
* outDecimal - the original inDecimal
* outDate - the original inDate
* outArray - an array of 10 dates, where outArray(n)
* is inDate+n days
*--------------------------------------------------------------
D i s 5u 0
D inChar s 10a
D inDate s d
D inDecimal s 10p 0
D outArray s d dim(10)
D outChar s 10a
D outDate s d
D outDecimal s 10p 0
D @LOWER c const('abcdefghijklmnop+
D qrstuvwxyz')
D @UPPER c const('ABCDEFGHIJKLMNOP+
D QRSTUVWXYZ')
C *entry plist
C parm inChar
C parm inDecimal
C parm inDate
C parm outChar
C parm outDecimal
C parm outDate
C parm outArray
C @UPPERLOWER xlate inChar outChar
C eval outDecimal = inDecimal
C eval outDate = inDate
C for i = 1 to %size(outArray)
C inDate adddur i:*days outArray(i)
C endfor
C return
Figure 1: RPG programs often take and receive aggregate data.
<html>
<body>
<%
// create then invoke the RPG JavaBean:
mc.Rpg001 rpg = new mc.Rpg001();
rpg.call("input", new java.math.BigDecimal("1.98", new java.sql.Date(new java.util.Date().getTime()));
java.util.Date dates[] = rpg.getOutArrayOfDates();
%>
outChar: <%=rpg.getOutChar() %><br>
outDate: <%=rpg.getOutDate() %><br>
outDecimal: <%=rpg.getOutDecimal() %><br>
Date array: <br>
<% for (int i = 0; i < 10; i++) { %>
<%= dates %><br>
<% } %>
</body>
</html>
Figure 2: A properly encapsulated JavaBean makes invoking RPG from JSP easy.
src="http://www.mcpressonline.com/articles/images/2002/DenoncourtV300.jpg" alt="http://www.mcpressonline.com/articles/images/2002/DenoncourtV300.jpg" border="0" >
Figure 3: The code within a JSP should present information, not manipulate it.
package mc;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.SQLException;import java.sql.Date;
import java.math.BigDecimal;
import java.util.Calendar;
/** Encapsulate access to RPG program RPG001 */
public class Rpg001 {
private static Connection con;
private static CallableStatement rpg;
private String outChar;
private BigDecimal outDecimal;
private Date outDate;
private java.util.Date outDateArray[] = new java.util.Date[10];
/**
create an Rpg001 object with an SQL connection,
create a SQL callable statement, and register
the output parameters
*/
public Rpg001 () {
if (con != null) return;
try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver"
} catch( ClassNotFoundException e) {
System.out.println("JDBC Driver not found: " + e);
System.exit(0);
}
try {
con = java.sql.DriverManager.getConnection ( "jdbc:as400://207.212.90.50", "denoncourt", "vo2max"
rpg = con.prepareCall("CALL denoncourt.rpg001 (?,?,?,?,?,?,?)"
rpg.registerOutParameter(4, java.sql.Types.CHAR);
rpg.registerOutParameter(5, java.sql.Types.DECIMAL);
rpg.registerOutParameter(6, java.sql.Types.DATE);
rpg.registerOutParameter(7, java.sql.Types.CHAR);
} catch (SQLException e) {
System.out.println("SQL error creating call statement: " + e);
return;
}
}
/** call the RPG program */
public void call(String inChar, BigDecimal inDecimal, java.sql.Date inDate)
throws SQLException {
rpg.setString (1, inChar);
rpg.setBigDecimal(2, inDecimal);
rpg.setDate(3, inDate);
rpg.executeUpdate();
outChar = rpg.getString(4);
outDecimal = rpg.getBigDecimal(5);
outDate = rpg.getDate(6);
String tenDates = rpg.getString(7);
Calendar cal = Calendar.getInstance();
// Stored Procedures don't handle dates well
// so we use an array of 10 character dates
// where the format is "CCYY-MM-DD"
for (int i = 0; i < 100; i += 10) {
String dateStr = tenDates.substring(i, i+10);
int ccyy = new Integer(dateStr.substring(0, 4)).intValue();
String temp = dateStr.substring(5, 7);
int mm = new Integer(dateStr.substring(5, 7)).intValue();
int dd = new Integer(dateStr.substring(8, 10)).intValue();
cal.set(ccyy, mm-1, dd-1);
if (i == 0 )
outDateArray[0] = cal.getTime();
else
outDateArray[i/10] = cal.getTime();
}
}
public String getOutChar()
{ return outChar;}
public Date getOutDate()
{ return outDate;}
public BigDecimal getOutDecimal()
{ return outDecimal;}
public java.util.Date[] getOutArrayOfDates()
{return outDateArray; }
// unit test
public static void main (String[] pList) {
Rpg001 app = new Rpg001();
try {
app.call("input", new BigDecimal("4.57", new java.sql.Date(new java.util.Date().getTime()));
} catch (SQLException e) {
System.out.println(e);
}
System.out.println("outChar: "+app.getOutChar()+"n"
System.out.println("outDate: "+app.getOutDate()+"n"
System.out.println("outDecimal: "+app.getOutDecimal()+"n"
java.util.Date dates[] = app.getOutArrayOfDates();
for (int i = 0; i < 10; i++) {
System.out.println(dates+"n"
}
System.exit(0);
}
}
Figure 4: Each RPG program you access from Java should be encapsulated with a JavaBean...
[ 本帖最后由 chenzhy 于 2006-4-18 18:33 编辑 ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
收藏。。。。。。。
Thank you ! i got it!
你可以排版一下的,在eclipse或者wsad里面format一下文件就好了,这看的头晕呀~
try { Class.forName("com.ibm.as400.access.AS400JDBCDriver"; } catch( ClassNotFoundException e)
这种连接方法好像已经不建议使用了,建议你可以看看jt400这个包,里面其实
有很多例子的,包括怎么远程调用400上的程序,怎么传递参数。