This example illustrates how to use Java beans generated by the
Program Call wizard to call an iSeries service program procedure from a Java
application.
The Program Call wizard generates two types of Java beans. One type is
a regular Java bean used by Java applications. This includes any standalone
Java application or Java components of Web applications such as servlets and
JSP pages. The other type can be used by the Web Services wizard to create
a Web service. This example uses the generated Java beans to call an iSeries
program procedure. You can also use the same service program and PCML from
this example to create a Web service. See Sample: Create and test an iSeries Web service.
Time required
Allow 15 minutes to run the example with the supplied data and to review
the output.
Before you begin
To run the examples, you need to
restore the WHOLESALE library on your iSeries host from the savefile wholesale.savf.
This savefile is in x:\WDSC\wdscsampl, where x is
the drive on which you have installed the product. See the Related reference
section Installing the sample libraries at the bottom of this page.
This example shows you how to call a service program
procedure from a Java application. The service program used in this example
is CWWSSRV, and the procedure is QryProdCost, which takes a product item number
as an input argument and returns the product price as an output argument.
- Open a Java Perspective.
- Create a new Java project with the name JavaProject.
- Select JavaProject and click the Create Program
Call Bean icon
in the workbench menu bar. This launches
the Program Call wizard.
- Click Add Program.
- Enter the following values in the Add Program page. You can click Browse to
look up the program that you want to call from your iSeries server.
Field |
Value |
Java bean name |
QueryProductCost |
Program object |
CWWSSRV |
Library |
WHOLESALE |
Program type |
*SRVPGM |
Entry point |
QryProdCost |
Return type |
void |
Thread safe |
false |
- Click OK. This defines the program or procedure
that you want to call.
- Enter the following values in the Add Parameter page:
Field |
Value |
Parameter name |
item |
Data type |
packed decimal |
Length |
5 |
Precision |
0 |
Usage |
input |
- Click OK. This defines the input parameter item,
which is a packed decimal with length 5 and precision 0 decimal places.
- Define another parameter by entering the following values in the Add
Parameter page:
Field |
Value |
Parameter name |
price |
Data type |
packed decimal |
Length |
7 |
Precision |
2 |
Usage |
output |
- Click OK. This defines output parameter price,
which is packed decimal with length 7 and precision 2 decimal places.
- Click Next to proceed to the next page of the wizard.
- Enter query.product in the Package field
to create the files in that package.
- Select Java application, and clear the Services check
box.
- Click Next to proceed to the third page of the
wizard and enter configuration information to be saved in the runtime configuration
file.
- Under the Authentication tab, the Generate
configuration file check box is selected by default. This saves
your settings in a runtime configuration file. You can keep the default filename.
Select Specify signon values and fill in your host
name, user ID and password. Enable password encoding is
selected by default, so your password is encoded in the runtime configuration
file.
- Under the Library list tab, in the Library field,
enter WHOLESALE and click Add.
In the Current Library field, enter *USRPRF.
- Click Finish to exit the wizard.
The following files are created in JavaProject:
- In the query.product package:
- In the root of JavaProject:
- defaultPCW.config
- QueryProductCost.mpcml
- QueryProductCost.pcml
QueryProductCost.java is the Java bean that can be used in your Java
application to call the iSeries program remotely. The Java bean generated
for the Java application normally consists of these methods:
- void setConnectionData(String hostname, String userid, String password)
- defines the host and sets the login user ID and password. This method implicitly
creates an AS400 object. Alternatively, you can explicitly create the AS400
object and use void setConnectionData(AS400 as400obj) to set the iSeries connection.
- setLibraryList - adds the runtime library list.
- setter methods - set the values of the input parameters.
- invoke - invokes the iSeries program or procedure.
- getter methods - get the output parameter values returned from the iSeries
program or procedure after the invocation.
- getAS400Object - returns the AS400 object of the current connection. You
can reuse this connection in your Java application to access other iSeries
resources for other purposes without creating another connection.
- disconnect - explicitly disconnects the iSeries connection. In any case,
the connection ends when your Java application terminates.
- setTraceEnabled - provides traces for debugging purposes.
- Xxx_Struct inner class - is used only if a structure is defined, where
Xxx is the name of the structure. Use the getter and setter methods of the
inner class to get and set the values of a structure.
To create the Java class with the sample code:
- Right-click the query.product package and select New
> Class.
- In the New Java Class wizard, enter QueryProductCostMain in
the Name field, select the check box for public
static void main(String[] args) under the Which method
stubs would you like to create? area, and click Finish to
create the file and open it in the editor view.
- Place the cursor at the end of
public static void main(String[] args) {
and press Enter to create a blank line.
- Start in the new blank line and add the following code prior to the closing
braces:
// Instantiate a QueryProductCost object.
QueryProductCost query = new QueryProductCost();
// The QryProdCost procedure takes item as an input,
// and the equivalent Java data type for a packed
// decimal is java.math.BigDecimal.
// The item number of the product to query is 1.
java.math.BigDecimal item = new java.math.BigDecimal(1);
query.setItem(item);
// Invoke the QryProdCost procedure on the iSeries server.
query.invoke();
// After invocation, QryProdCost returns the price of
// item number 1 as a packed decimal, which is
// equivalent to the Java data type java.math.BigDecimal.
java.math.BigDecimal price = query.getPrice();
// Print the price.
System.out.println("Price: "+price.toString());
// Disconnect the host connection.
query.disconnect();
System.exit(0);
- Right-click anywhere in the editor pane and select Save to
save your changes.
- In the Package Explorer view, select QueryProductCostMain.java under query.product.
- In the menu bar, click the down arrow next to the Run icon
and click .
- In the signon dialog, enter host name, your userid and password. The price
of item 1 is displayed in the console.
You have successfully called a service program procedure from a Java
application.
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.