You are on page 1of 54

Getting Started with Java and AS/400: Client/Server Java

Getting Started with Java and AS/400: Client/Server Java

Copyright International Business Machines Corporation 1998, 1999. All rights reserved. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

Contents
Chapter 1. What can you do with Java and AS/400? . . . . . . . . . . . . . 1 Chapter 2. Setting up client/server Java
AS/400 Toolbox for Java overview . PTFs for Java and AS/400 . . . . Whats new for V4R5 . . . . . . . . . . . . . . . . . . . . Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win NT mapped drive) Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win NT copied file) . . Setting your PATH and CLASSPATH environment variables (Windows 95 or 98) . . . . . . . . Opening AUTOEXEC.BAT . . . . . . . . Updating your PATH statement for JDK (Win 95/98) . . . . . . . . . . . . . . . Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win 95/98 mapped drive) . . . . . . . . . . . . . . . Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win 95/98). . . . . Installing your IDE product . . . . . . . . . 22 24 26 26 26

3
. 4 . 4 . 5

Chapter 3. Setting up your AS/400 for client/server Java . . . . . . . . . . 7


Installing the licensed programs for client/server Java . . . . . . . . . . . . . . . . . 7 Installing a licensed program with the Restore Licensed Program command for client/server Java . 8 Starting the OS/400 Servers . . . . . . . . . 8 Check QUSER profile . . . . . . . . . . . 9 Change the QUSER user profile . . . . . . 10 AS/400 Toolbox for Java record-level access classes . . . . . . . . . . . . . . . 11

27 28 29

Chapter 5. Creating a Java application


Writing a Java application . . . . . . . . . AS/400 Toolbox for Java CommandCall Example Naming your Java application . . . . . . . Compiling your Java application . . . . . . . Running your Java application . . . . . . . . Running the CommandCallExample . . . . . Examples: Database interaction with Java . . . . Example: AS/400 database interaction through JDBC . . . . . . . . . . . . . . . Example: GUI interaction with the AS/400 database . . . . . . . . . . . . . .

31
31 32 33 34 35 35 36 36 39

Chapter 4. Setting up your workstation


Installing the Java 2 Software Development Kit on your workstation . . . . . . . . . . . Setting up access to AS/400 Toolbox for Java . . Mapping a network drive to AS/400 for client/server Java . . . . . . . . . . Downloading the jt400 file on your workstation Copying the jt400.zip file to your workstation, using FTP . . . . . . . . . . . . . Setting your PATH and CLASSPATH environment variables . . . . . . . . . . . . . . Setting your PATH and CLASSPATH environment variables (Windows NT) . . . Updating your PATH statement for JDK (Win NT) . . . . . . . . . . . . . . .

13
. 13 . 14 . 14 16 . 17 . 19 . 19 . 20

Chapter 6. Other information about Java and AS/400 . . . . . . . . . . 41


Getting Started with Java and AS/400: More servlet resources . . . . . Java and AS/400 resources . . . . Java education . . . . . . . . Servlets . . . . . . . . . . . . . . . . . 41 42 42 44

Copyright IBM Corp. 1998, 1999

iii

iv

Getting Started with Java and AS/400: Client/Server Java

Chapter 1. What can you do with Java and AS/400?


Java has the potential to connect users with information regardless of its source. Whether the data comes from the web, a database, an integrated file system, or any other entity, Java can control it. Using Java with your AS/400 opens a new world of flexibility and manageability. You can create applications that run from just about anywhere, and still provide access to your AS/400 resources. You can use the web, Java, and AS/400 together with ease. This list highlights only some of the many advantages Java and AS/400 have to offer. Internet applications v Use the web, Java, and AS/400 together with servlets. v Provide secure connections to AS/400. v Create dynamic HTML with AS/400 Toolbox for Java HTML classes. v Create Java applets. AS/400 applications v Call AS/400 programs and service programs. v Perform administrative tasks with AS/400 Toolbox for Java utility classes. v Create client/server database applications with the AS/400 Toolbox for Java JDBC drivers. v Create native AS/400 database applications with the AS/400 Developer Kit for Java JDBC drivers. v Access legacy AS/400 data and applications. v Create optimized Java code for specific AS/400 operations using the AS/400 Developer Kit for Java. v Create client/server Java applications with the AS/400 Toolbox for Java. v Easily create graphical user interfaces (GUIs) with the Graphical Toolbox, a visual editing tool. (Uses the AS/400 Toolbox for Java) Java in general v Create graphical user interfaces (GUIs) to present and request data from users. v Enjoy the benefits of Object-Oriented programming with Java. (i.e. code reuse, inheritance, abstraction) v Use Java with other languages, such as C++ and RPG. v Enjoy Javas inherent features such as, garbage collection, security, and platform independence.

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Copyright IBM Corp. 1998, 1999

Getting Started with Java and AS/400: Client/Server Java

Chapter 2. Setting up client/server Java


Client/server Java programming with AS/400 consists of a Java program running on a workstation (client) that accesses data and resources on AS/400 (server). The client/server enabler for Java and AS/400 is AS/400 Toolbox for Java, a no-cost licensed program on your AS/400. Setup requirements: v Any client with a compliant Java virtual machine. AS/400 Toolbox for Java works with Windows 95/98/NT, OS/2, Linux, AIX, Sun Solaris and AS/400 (As a client). However, for simplicity, the information in this setup guide assumes you have a Win32 workstation (A PC running Windows 95, 98, or NT). v TCP/IP installed and configured on your AS/400 and workstation. For more information about TCP/IP, see TCP/IP Configuration Fastpath and OS/400 TCP/IP Configuration and Reference. To get started with client/server Java programming for AS/400, follow these steps: 1. Setting up your AS/400. a. Installing the licensed programs for Java. v AS/400 Toolbox for Java v OS/400 Host Servers b. Starting the OS/400 Host Servers and DDM server. 2. Setting up your workstation. 3. a. Installing the Java 2 Software DevelopmentKit (J2SDK).

b. Setting up access to AS/400 Toolbox for Java. v Mapping a network drive to AS/400 Toolbox for Java, or v Copying AS/400 Toolbox for Java files to your workstation. Setting your PATH and CLASSPATH environment variables. c. d. Installing your integrated development environment (IDE) product. 4. Creating a Java application. 5. a. Writing a Java application, and saving it. b. Compiling your Java application, using the javac command. c. Running your Java application, using the java command. Continuing with Java and AS/400 Use the Javadocs to make the most out of Java and AS/400. Describing each class in detail, the Javadocs are a great AS/400 Toolbox for Java reference. Also, see the AS/400 Toolbox for Java programming guide for all the resources, tips, and information you need while creating your Java applications. To stay current with the latest technologies and innovations, see Other Information about Java and AS/400.

Copyright IBM Corp. 1998, 1999

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

AS/400 Toolbox for Java overview


AS/400 Toolbox for Java is a set of Java classes that allow you to access AS/400 data through a Java program. When running on a client, AS/400 Toolbox for Java classes make TCP/IP socket connections to your AS/400 system and accesses resources on AS/400 through the OS/400 Host Servers. With AS/400 Toolbox for Java, a Java application can work with AS/400 programs, commands, spooled files, DB2/400 data, messages, jobs, and other AS/400 resources. The application can run on a workstation or on the AS/400 Java virtual machine. The AS/400 Toolbox for Java is a no charge Licensed Program distributed with every AS/400. For more information on the capabilities of AS/400 Toolbox for Java, see the AS/400 Toolbox for Java product documentation.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

PTFs for Java and AS/400


For those of you new to the AS/400 platform, a PTF, or program temporary fix is a temporary solution to or bypass of a problem diagnosed by IBM as resulting from a defect in a current unaltered release of a licensed program. PTFs are designed to replace one or more objects in the product. Generally, PTFs are incorporated in a future release of the product. During your deployment cycle to the AS/400 system, it is a good idea to keep an eye on which PTFs are needed to run your product successfully. Your customers will also need to know this information. Therefore, it is highly recommended that you have the latest PTFs installed and applied on your AS/400 system. Make sure you visit the following links for the most current information: v AS/400 Toolbox for Java PTFs v WebSphere Application Server PTFs Also, it is recommended that you request every 2 months or so the latest Group PTF for the following licensed products:
OS/400 release V4R2 Product(s) 5769-999 (SLIC) 5769-SS1 (XPF) 5769-JV1 (Java) 5769-999 (SLIC) 5769-SS1 (XPF) 5769-JV1 (Java) 5769-999 (SLIC) 5769-SS1 (XPF) 5769-JV1 (Java) Group PTF SF99065

V4R3

SF99066

V4R4

SF99067

Getting Started with Java and AS/400: Client/Server Java

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Whats new for V4R5


Install Java 2 Software Development Kit (J2SDK) V4R5 works with the Java 2 Software Development Kit which includes the JDK 1.2 and Swing 1.1 classes. New examples Two code examples were added to show how Java can interact with the AS/400 database. What can you do with Java and AS/400? A new section titled What can you do with Java and AS/400? was added. How to see your current position in our information: The small links at the top of each page show you where you are in our information. These links are displayed in a directory structure format. To see where you are, read through the links. For example, if you look at the top of this page, you see: Getting started with Java and AS/400 \ Whats new for V4R5 These links show that you are in the Whats new for V4R5 information under the Getting Started with Java and AS/400 topic. How to see whats new or changed: To help you see where technical changes have been made, this information uses: v The v The image to mark where new or changed information begins. image to mark where new or changed information ends.
[ Legal | AS/400 Glossary ]

[ Information Center Home Page | Feedback ]

Chapter 2. Setting up client/server Java

Getting Started with Java and AS/400: Client/Server Java

Chapter 3. Setting up your AS/400 for client/server Java


To access your AS/400 system with Java, install AS/400 Toolbox for Java. AS/400 Toolbox for Java uses the OS/400 Host Servers, so you will need to install and start the servers. Follow these steps: 1. Installing the licensed programs (LPs) for Java. v AS/400 Toolbox for Java v OS/400 Host Servers 2. Starting the OS/400 Host Servers and the DDM server.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Installing the licensed programs for client/server Java


In order to program client/server Java with AS/400, you must install AS/400 Toolbox for Java. You also need to install the OS/400 Host Servers, which AS/400 Toolbox for Java uses to access AS/400 resources. 1. At the AS/400 command prompt, type: GO LICPGM Press the Enter key. The Work with Licensed Programs display appears. 2. Select menu option 11 to install the licensed programs. 3. Scroll through the list by pressing the PageDown key. Find these two licensed programs and, if they have not been installed, type 1 (Install) in the Option blank in front of both:
5769SS1 OS/400 - Host Servers 5769JC1 AS/400 Toolbox for Java Note: To find out if a licensed program has been installed, press F11 on the Install Licensed Programs display. An installed licensed program displays *COMPATIBLE in the Installed Status field. If one or both of these programs does not appear on the Install Licensed Programs menu at all, you will need to use the restore licensed programs (RSTLICPGM) method to install the licensed programs from CD-ROM, disk, or tape.

4. Press the Enter key. The programs will begin installing. When the licensed programs are finished installing, the Work with Licensed Programs display appears again.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Copyright IBM Corp. 1998, 1999

Installing a licensed program with the Restore Licensed Program command for client/server Java
The programs listed in the Install Licensed Programs display are those supported by the LICPGM installation when your system was new. Occasionally, new programs become available which are not listed as licensed programs on your system (for instance, AS/400 Toolbox for Java for V3R2, V3R7, and V4R1). If this is the case with the program you want to install, you must use the Restore Licensed Program (RSTLICPGM) command to install it. To install a licensed program with the Restore Licensed Program (RSTLICPGM) command, follow these steps: 1. Put the tape or CD-ROM containing the licensed program in the appropriate drive. 2. On the AS/400 command line, type: RSTLICPGM and press the Enter key. The Restore Licensed Program (RSTLICPGM) display appears. 3. In the Product field, type the ID number of the licensed program you want to install. 4. In the Device field, specify your install device.
Note: If you are installing from a tape drive, the device ID is usually in the format TAPXX, where XX is a number, like 01.

5. Keep the default settings for the other parameters in the Restore Licensed Program display. Press the Enter key. 6. More parameters appear. Keep these default settings also. Press the Enter key. The program will begin installing. When the licensed program is finished installing, the Restore Licensed Programs display appears again.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Starting the OS/400 Servers


Before you can access data on your AS/400 with AS/400 Toolbox for Java, you must start the Host Servers. Starting the Host Servers initiates a server daemon that listens for and accepts a connection request from a client system (in this case, your workstation). The host server daemon also attaches your client connection to the appropriate server job. To start the servers, follow these steps: 1. Check that the QUSER profile is enabled. 2. Run the Start Host Servers command. On the AS/400 command line, type: STRHOSTSVR *ALL Press the Enter key.
Note: You may receive a message, Host server daemon jobs unable to communicate using IPX,. This message means that IPX has not been configured. The message does not mean that the Host Servers have not started.

Getting Started with Java and AS/400: Client/Server Java

3. If you plan to use the record-level access classes of AS/400 Toolbox for Java, the DDM (distributed data management) server must be running. The DDM server processes record-level data access requests. On the AS/400 command line, type: STRTCPSVR *DDM Press the Enter key. A message appears at the bottom of your screen, stating DDM server starting.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Check QUSER profile


The OS/400 Host Servers start under the QUSER user profile. So, before you try to start the servers, its a good idea to make sure that the QUSER profile is functional. To check QUSER profile, follow these steps: 1. On an AS/400 command line, type: WRKUSRPRF USRPRF(QUSER) Press the Enter key. Use Tab or the Down Arrow to position the cursor in the Opt blank in front of QUSER. Type 5 to display the user profile, and press the Enter key. A display, similar to the one below, will appear.

Chapter 3. Setting up your AS/400 for client/server Java

2. Make sure that the Status field reads *ENABLED and that the Set password to expired field reads *NO. If the profile is not *ENABLED and/or the password is expired, change the profile.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Change the QUSER user profile


If the QUSER profile is disabled, you must enable it to start the OS/400 Host Servers. Also, the QUSER profile password cannot be *NONE. If this is true, you must reset it. Follow these steps to fix the QUSER profile: 1. Press F3 or F12 to return to the Work with User Profiles display. 2. Use Tab or the Down Arrow to position the cursor in the Opt blank in front of QUSER. Type 2 to change the user profile, and press the Enter key. 3. Change these fields to these values:
Set password to expired . . . . Status . . . . . . . . . . . . . *NO *ENABLED

4. Press the Enter key.

10

Getting Started with Java and AS/400: Client/Server Java

A message appears at the bottom of your display, stating User profile QUSER changed. The QUSER user profile is now ready to start the OS/400 Host Servers.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

AS/400 Toolbox for Java record-level access classes


The record-level access classes allow your Java program to create and work with database records, physical and logical files, and file members that reside on your AS/400. For more information on record-level access, see the AS/400 Toolbox for Java product documentations Record-level access topic.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Chapter 3. Setting up your AS/400 for client/server Java

11

12

Getting Started with Java and AS/400: Client/Server Java

Chapter 4. Setting up your workstation


Now that your AS/400 is ready for Java, set up your Win32 workstation to run Java. Follow these steps to prepare your workstation: Installing the Java 2 Software Development Kit (J2SDK). 1. 2. Setting up access to AS/400 Toolbox for Java jt400 file. 3. Setting your PATH and CLASSPATH environment variables. 4. Installing your integrated development environment (IDE) product.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Installing the Java 2 Software Development Kit on your workstation


You will need a Java Development Kit (JDK) to provide your workstations Java environment. You will be using the JDK and Swing classes with the AS/400 Toolbox for Java classes to create your Java programs. Swing is a library of graphical user interface components that support the AS/400 Toolbox for Java graphical interfaces. Both the JDK 1.2 and Swing 1.1 classes are included with the Java 2 Software Development Kit. 1. Download the J2SDK

Note: Follow the directions on the Sun Microsystems, Inc. site to download the JDK. Make sure to carefully read all licensing agreements.

2. After you have finished downloading the J2SDK, follow the To continue instructions located below the purple download box to install the JDK. 3. Make sure you select ZIP file for Download the J2SDK documentation Windows in the Select Download Format pull-down window.

You need a ZIP file extraction program to install the JDK documentation files. For more information about ZIP extraction programs, see the JDK documentation installation instructions .

4. After you have downloaded the JDK documentation ZIP file, follow the To continue instructions located below the purple download box to install the documentation.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Copyright IBM Corp. 1998, 1999

13

Setting up access to AS/400 Toolbox for Java


Note: You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file.

If you are familiar with the workings of a Java Development Kit, you know that the classes.zip file contains the core Java classes of the Java platform. AS/400 Toolbox for Java also has a core classes filejt400.zip. To compile and run Java programs containing AS/400 Toolbox for Java classes, your workstation must have access to AS/400 Toolbox for Java and jt400.zip. The methods described below each accomplish the same goal; they set up access to the jt400.zip. However, each method provides its unique advantages. Choose the method that best suits your needs. You can choose from three basic methods of accessing jt400.zip: 1. Recommended if you have AS/400 Client Access: You can remotely access jt400.zip by mapping a network drive to your AS/400 (using AS/400 Client Access). 2. Recommended if you do not have AS/400 Client Access: a. You can copy the jt400.zip file to your workstation, using FTP. b. Or, download and install the jt400.zip on your workstation.

Note:The problem with these methods is that when the AS/400 copy is updated through PTFs, the copy on your workstation will become down-level. However, you can write a program with the AS400ToolboxInstaller class to keep your workstation copy of jt400.zip up-to-date.

3. Recommended if you want to install the least amount of AS/400 Toolbox for Java classes on your client: See the Proxy Support page in the AS/400 Toolbox for Java documentation.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Mapping a network drive to AS/400 for client/server Java


For these steps, you need AS/400 Client Access for Windows installed on your AS/400 and on your workstation. For more information on how to install and configure AS/400 Client Access, see the AS/400 Technical Studio Client Access Workshop. You must have a connection configured for the AS/400 system before you can map a network drive. 1. Open Windows Explorer: a. Right-click on the Start button on your Windows taskbar. b. Click Explore in the pop-up menu. 2. Select Map Network Drive from the Tools pulldown menu.

14

Getting Started with Java and AS/400: Client/Server Java

3. Select the drive (for example, X:) you want to connect to your AS/400. 4. Type the path name to the your AS/400 system. For example: \\MYAS400 where MYAS400 is the name of your AS/400 system.

Chapter 4. Setting up your workstation

15

5. If the Reconnect at logon checkbox is empty, click on it. 6. Click OK to finish. Your mapped drive will appear in the All Folders section of Windows Explorer.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Downloading the jt400 file on your workstation


A fully functional evaluation copy of the AS/400 Toolbox for Java code is available for download at the following website: 1. Download the jt400 file.
Note: You may want to create a new directory for this file. To create a new directory, when prompted with the Save As... window, click the New Folder icon. Type the name of the directory (example: jt400) and click OK. This creates a directory named jt400 and saves the file inside it.

2. Unzip the jt400 file

16

Getting Started with Java and AS/400: Client/Server Java

Note: By unzipping this file, two things occur: v New files are extracted v A lib subdirectory is created and all the new, extracted files are automatically saved there. This is important to keep in mind for future reference. Especially because the next step involves setting the CLASSPATH environment variable for the jt400.zip file. Example: If you saved the jt400All.zip file in your C:\jt400 directory, then the jt400.zip file will be saved in the C:\jt400\lib directory.

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Copying the jt400.zip file to your workstation, using FTP


You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file. To provide JavaSofts JDK with access to AS/400 Toolbox for Javas core classes, copy jt400.zip from your AS/400 to your workstation using FTP (file transfer protocol): 1. On your workstation, open an MS-DOS session. 2. Create a directory on your workstation for jt400.zip. 3. At the DOS prompt, type: ftp myas400 where myas400 is the host name of your AS/400.
Note: If your AS/400 does not have a host name, use your AS/400s IP address instead of a host name.

A message appears, similar to Connected to myas400.company.com.. 4. You will be prompted for your User name for your AS/400. Type it, and press the Enter key. 5. You will be prompted for your Password for your AS/400. Type it, and press the Enter key. A message will appear, similar to 230 USER logged on, where USER is your AS/400 User ID. 6. Specify binary transfer mode. At the DOS prompt, type: bin and press the Enter key. A message will appear, stating 200 Representation type is binary IMAGE. 7. Navigate to the jt400 directory on your AS/400. Type: cd /QIBM/ProdData/HTTP/Public/jt400/lib and press the Enter key.
Note: If you have moved the jt400.zip file or modified this directory structure since installing AS/400 Toolbox for Java, specify the new location of jt400.zip.

Chapter 4. Setting up your workstation

17

A message will appear, stating 250 /QIBM/ProdData/HTTP/Public/jt400/lib is current directory. 8. Get the jt400 file. Type: mget jt400.zip and press the Enter key. A prompt, mget jt400.zip? will appear. Type y and press the Enter key. Two messages will appear, stating 200 PORT subcommand request successful and 150 Retrieving file /QIBM/ProdData/HTTP/Public/jt400/lib/jt400.zip. After a few moments, two more messages appear, 250 File transfer completed successfully and a message similar to xxxxxx bytes received in 22.08 seconds (179.57 Kbytes/sec). These messages signal the successful completion of FTP. 9. End the FTP session. Type: quit and press the Enter key. 10. End the MS-DOS session. Type exit and press the Enter key.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Creating a directory on your workstation


Follow these steps to create a directory on your workstation. 1. Your MS-DOS prompt probably reads C:\Windows>, where C: is the drive on which Windows is installed. If this is the drive on which you would like to have AS/400 Toolbox for Java located, type cd\ and press the Enter key. If you would like AS/400 Toolbox for Java on a different drive (assuming you have one), type X:, where X: is the letter of the drive you want, and press the Enter key. 2. Create a new directory. Type: mkdir jt400 where jt400 is the name of the directory.
Note: To avoid confusion, use jt400 as your directory name. This is the name of the AS/400 directory that contains the AS/400 Toolbox for Java files.

Press the Enter key. 3. Make the new directory your current directory. Type: cd jt400 and press the Enter key. The prompt changes from X:\> to X:\jt400> (where X: is the letter of your hard drive). The directory has been successfully created. Return to the previous page and continue with Step 3.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

18

Getting Started with Java and AS/400: Client/Server Java

Setting your PATH and CLASSPATH environment variables


To compile a Java application that includes AS/400 Toolbox for Java and Swing classes, you would have to type something like this at the MS-DOS prompt (Using JDK 1.2.2):
C:\jdk1.2.2\bin\javac -classpath .;C:\jt400\lib\jt400.zip myfile.java

Typing commands of this length can be tedious and error-prone. Therefore, you will want to set your workstations PATH and CLASSPATH environment variables to simplify your Java commands. In this example, C:\jdk1.2.2\bin\javac tells the system where to find the Java compiler. If the PATH variable is set to point at the JDK utilities directory, C:\jdk1.2.2\bin, only the utility name is required. In this case, typing javac. The CLASSPATH variables tell the Java compiler and runtime where to find your class libraries (AS/400 Toolbox for Javas jt400.zip or jt400.jar). With the CLASSPATH set, you do not have to use the -classpath parameter in your commandsthe compiler and runtime will search automatically for the class libraries. So, with the PATH and CLASSPATH variables set, the long command becomes:
javac myfile.java

For the Win32 platforms, there are two distinct ways to set environment variables. In Windows 95 and 98, you can set your PATH and CLASSPATH values by editing your AUTOEXEC.BAT file. Windows NT offers a dialog window for entering the values. To set your programming environment variables, click on your workstations operating system: v Windows NT v Windows 95/98
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Setting your PATH and CLASSPATH environment variables (Windows NT)


Follow these steps to configure your workstation environment variables for Java: 1. Open your NT systems environment settings: a. b. c. d. Double-click the My Computer icon on your desktop. Double-click the Control Panel icon in the My Computer folder. Double-click the System icon in the Control Panel folder. Click the Environment tab in the System Properites window.

Chapter 4. Setting up your workstation

19

2. Updating your PATH statement for the JDK. 3. Updating your CLASSPATH statement for AS/400 Toolbox for Java: v If you have mapped a network drive to your AS/400 v If you have copied jt400.zip to your workstation 4. After you have finished updating your systems environment settings, click the Apply button. Your new settings will now be in effect. 5. Click the OK button to exit the System Properties window.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Updating your PATH statement for JDK (Win NT)


Add references to the JDK to your PATH statement, located in the Environment panel of your System Properites window:

20

Getting Started with Java and AS/400: Client/Server Java

1. In the System Variables window, find the variable Path and click it. Notice that the variable and its value appear in the two text fields at the bottom of the display.

2. At the end of the text in the Value field, add: ;X:\JDK1.2.Y\BIN where X is the drive on which the JDK is installed, and Y is the version number of JDK 1.2 that you installed. This action sets the PATH variable to point to the Java utilities directory. When you run a Java command, such as java or javac, from an MS-DOS prompt, your system will automatically locate the utility that the command invokes. In the example below, JDK 1.2.2 is installed on the C: drive. The brown text shows what was added to the Path value.

Chapter 4. Setting up your workstation

21

Click the Set button. The Variable and Value text fields clear, and the new value is added to the system variables.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win NT mapped drive)
Add references to AS/400 Toolbox for Java to your CLASSPATH statement, located in the Environment panel of your System Properites window. You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file.

22

Getting Started with Java and AS/400: Client/Server Java

1. Check that the Variable text field reads ClassPath. If it does not, click on ClassPath in the System Variables window. 2. At the end of the Value field, type: ;X:\QIBM\ProdData\HTTP\Public\jt400\lib\jt400.zip where X: is the name of the drive you mapped to your AS/400. This action sets your workstations CLASSPATH variable to the AS/400 Toolbox for Java class library, jt400.zip. When you compile or run a Java application that contains AS/400 Toolbox for Java classes, the system will automatically locate jt400.zip. In the example below, the X: drive is mapped to an AS/400. The brown text shows what was added to the CLASSPATH Value field.

If you cannot find a ClassPath value, add it. a. In the Variable field, type: ClassPath
Chapter 4. Setting up your workstation

23

b. In the Value field, type: .;X:\QIBM\ProdData\HTTP\Public\jt400\lib\jt400.zip where X is the name of the drive you mapped to your AS/400. 3. Click the Set button. The Variable and the Value fields clear, and the new values appear in the System Variables window.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win NT copied file)
Add references to AS/400 Toolbox for Java to your CLASSPATH statement, located in the Environment panel of your System Properites window. You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file.
Note: The CLASSPATH environment variable must be set to reflect where you copied the jt400.zip file. If you are unsure of the location, do a search. Click on the Start button in your Windows NT Taskbar. Go to Find, and then to Files or Folders... type: jt400.zip and press Enter. Set your CLASSPATH accordingly by using the instructions below as a guide.

1. Check that the Variable text field reads ClassPath. If it does not, click on ClassPath in the System Variables window. 2. At the end of the Value field, type: ;X:\jt400\jt400.zip where X: is the drive on which you copied jt400.zip, and jt400 is the name of the directory in which you saved the copy of jt400.zip. This action sets your workstations CLASSPATH variable to the AS/400 Toolbox for Java class library, jt400.zip. When you compile or run a Java application that contains AS/400 Toolbox for Java classes, the system will automatically locate jt400.zip. In the example below, jt400.zip is copied to a directory (JT400) on the C: drive. The brown text shows what was added to the CLASSPATH Value field.

24

Getting Started with Java and AS/400: Client/Server Java

If you cannot find a ClassPath value, add it. a. In the Variable field, type: ClassPath b. In the Value field, type: .;X:\jt400\jt400.zip where X is the drive on which you copied jt400.zip, and jt400 is the name of the directory in which you saved the copy of jt400.zip. 3. Click the Set button. The Variable and the Value fields clear, and the new values appear in the System Variables window.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Chapter 4. Setting up your workstation

25

Setting your PATH and CLASSPATH environment variables (Windows 95 or 98)


Follow these steps to configure your workstation environment variables for Java: 1. Opening your AUTOEXEC.BAT file for editing. AUTOEXEC.BAT contains your workstations environment variable settings. 2. Updating your PATH statement for the JDK. 3. Updating your CLASSPATH statement for AS/400 Toolbox for Java: v If you have mapped a network drive to your AS/400 v If you have copied jt400.zip to your workstation 4. The AUTOEXEC.BAT file sets the environment variables at startup. You can use one of several methods to reset your environment variables during a session. Its recommended that you reboot your workstation.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Opening AUTOEXEC.BAT
Your workstations AUTOEXEC.BAT file contains the programming environment variables. When you start your workstation, the AUTOEXEC.BAT file sets your workstations environment. Follow these steps to open your workstations AUTOEXEC.BAT file. 1. Click on the Start button in the lower left corner of your desktop. 2. Position your mouse over Find.... Select Files or Folders from the Find menu. 3. In the Named field, type autoexec.bat. In the Look in field, select the drive on which Windows 95 is located. Click on the Find Now button. 4. In the bottom window, right-click on Autoexec.bat.
Note: More that one Autoexec.bat file may be found. More than likely, the first instance is the one you want. Ignore any occurrance that is located in the backup directory structure.

5. Select Edit from the pop-up menu. If you are prompted to choose a program, pick Notepad.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Updating your PATH statement for JDK (Win 95/98)


Follow these steps to add the JDK utilities to your workstations programming environment: 1. On a new line in your AUTOEXEC.BAT file, near the beginning of the file, type: SET JDK=X:\JDK1.2.Y where X: is the name of the drive on which you have the JDK installed, and Y is the version of JDK you have installed. This action sets the variable JDK with the path to your JDK directory. If, in the future, you install a different JDK, you need only to change this line to change your PATH and CLASSPATH variables for your new JDK. 2. At the end of the SET PATH line, type:

26

Getting Started with Java and AS/400: Client/Server Java

;%JDK%\BIN This action sets the PATH variable to point to the Java utilities directory. When you run a Java command, such as java or javac, from an MS-DOS prompt, your system will automatically locate the utility that the command invokes. The example below shows, in brown, a modified AUTOEXEC.BAT file which now includes a JDK in the environment variables. The black text shows how the AUTOEXEC.BAT file originally read. Below, the JDK 1.2.2 is installed on the C: drive. Your workstations setup may differ, depending on your version of JDK and the drive on which it is installed.

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win 95/98 mapped drive)
Follow these steps to add the AS/400 Toolbox for Java class file to your workstations programming environment: You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file. 1. On a line in your AUTOEXEC.BAT file, type: SET JT400=X:\QIBM\ProdData\HTTP\Public\jt400 where X: is the name of the drive you mapped to your AS/400. This action sets the variable JT400 with the path to your AS/400 Toolbox for Java directory. Since the path name of the jt400 directory is rather long, the variable JT400 will act as a shorthand way to write the path. 2. On a new line, type: SET CLASSPATH=.;%JT400%\lib\jt400.zip

Chapter 4. Setting up your workstation

27

This action sets your workstations CLASSPATH variable to the AS/400 Toolbox for Java class library, jt400.zip. When you compile or run a Java application that contains AS/400 Toolbox for Java classes, the system will automatically locate jt400.zip.
Note: The period (.) in the CLASSPATH statement specifies the current directory. With this setting, you do not have to specify the full path name (which includes drive and directory structure) of your Java application to compile or run it.

The example below shows, in brown, a modified AUTOEXEC.BAT file which now includes the AS/400 Toolbox for Java (residing on AS/400) in the environment variables. The X: drive is mapped to the AS/400 system.

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Updating your CLASSPATH statement for AS/400 Toolbox for Java (Win 95/98)
Follow these steps to add the AS/400 Toolbox for Java class file to your workstations programming environment: You can use either the jt400.zip file or the jt400.jar file on your workstation. The following instructions use the jt400.zip file, but these instructions also work for the jt400.jar file. 1. On a new line in your AUTOEXEC.BAT file, below the SET JDK type: SET JT400=X:\jt400 where X: is the drive and jt400 is the directory in which the AS/400 Toolbox for Java jt400.zip file resides. This action sets the variable JT400 with the path to your AS/400 Toolbox for Java directory. If, in the future, you decide to move jt400.zip or map a network drive to your AS/400s copy of jt400.zip, you need only to change this line to change your CLASSPATH variables for AS/400 Toolbox for Java. 2. On a new line, type:

28

Getting Started with Java and AS/400: Client/Server Java

SET CLASSPATH=.;%JT400%\jt400.zip This action sets your workstations CLASSPATH variable to the AS/400 Toolbox for Java class library, jt400.zip. When you compile or run a Java application that contains AS/400 Toolbox for Java classes, the system will automatically locate jt400.zip.
Note: The period (.) in the CLASSPATH statement specifies the current directory. With this setting, you do not have to specify the full path name (which includes drive and directory structure) of your Java application to compile or run it.

The example below shows, in brown, a modified AUTOEXEC.BAT file which now includes the AS/400 Toolbox for Java in the environment variables. Below, the AS/400 Toolbox for Java is installed on the C: drive.

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Installing your IDE product


If you have not programmed with an IDE, you might want to check one out. IDEs are PC applications that have a sophisticated programming environment built in, which typically includes compiling and debugging utilities. Many IDEs feature graphical-based code generationwhich means you can rapidly produce source code with just a few clicks of your mouse. You can purchase any of a number of Java-specific IDEs which usually have a Java Development Kit (JDK) already integrated into the product. The following links take you to some popular IDEs currently available: v IBM Visual Age for Java for AS/400. v Borland JBuilder 3 v Sybase PowerJ
Chapter 4. Setting up your workstation

29

v Symantec Visual Caf v NetBeans Developer v Oracle JDeveloper v Metrowerks CodeWarrior Professional . If you are planning to use an integrated development environment (IDE), nows the time to install it. See the instructions that came with your product for setup information. If you do not have the resources for an IDE, however, you can still program with a text-based editor and the DOS prompt.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

30

Getting Started with Java and AS/400: Client/Server Java

Chapter 5. Creating a Java application


Use any of several different ways to create a Java application on your workstation. As long as your code runs, you can use whatever method you prefer. This example demonstrates one of the more basic methods of creating a Java applicationusing Windows Notepad, the JDK, the MS-DOS prompt, and AS/400 Toolbox for Java. Even if you plan to develop your Java code in a more sophisticated way, you might want to follow along to get a feel for some of the general rules of creating Java applications.
Note: If you are using an integrated programming application like IBM VisualAge for Java or Borlands JBuilder, follow the instructions that came with the product to create a Java application.

To create a Java application, follow these steps: 1. Writing a Java application. 2. Compiling your application. 3. Running your application. Client/server examples and graphical interfaces Accessing your data through a graphical interface is important. Follow the links below to see some of the many ways Java makes this possible. Example database applications Creating graphical user interfaces (GUIs) The Graphical Toolbox, a visual editing tool, uses the AS/400 Toolbox for Java. AS/400 Toolbox for Java GUI classes AS/400 Toolbox for Java provides a set of graphical user interface (GUI) classes.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Writing a Java application


Follow these steps to write a Java application for your workstation: 1. Open an MS-DOS session. 2. At the DOS prompt, type: notepad and press the Enter key. This action opens a Notepad session. 3. Type your Java application in the Notepad window.
Note: When naming your Java applications make sure your file name matches your class name. Also make sure to be consistent with your capitalization. Java is case sensitive. If you would like to use a Java code example instead, see the AS/400 Toolbox for Java CommandCallExample. Copy the CommandCallExample application into your Notepad window.

Copyright IBM Corp. 1998, 1999

31

4. Save your application by clicking the File menu, and then clicking Save As.... In the Save As... display, navigate to the directory in which you want to save your Java application. Type the Java application name in the File name: blank. Make sure the Save as Type: window shows All files (*.*).

5. Click the Save button.


[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

AS/400 Toolbox for Java CommandCall Example


////////////////////////////////////////////////////////////////////////////////// // // Command call example. This program prompts the user // for the name of the AS/400 system and the command to run, then // prints the result of the command. // ////////////////////////////////////////////////////////////////////////////////// // // This source is an example of AS/400 Toolbox for Java CommandCall // IBM grants you a nonexclusive license to use this as an example // from which you can generate similar function tailored to // your own specific needs. // // This sample code is provided by IBM for illustrative purposes // only. These examples have not been thoroughly tested under all // conditions. IBM, therefore, cannot guarantee or imply // reliability, serviceability, or function of these programs. // // All programs contained herein are provided to you AS IS // without any warranties of any kind. The implied warranties of // merchantablility and fitness for a particular purpose are // expressly disclaimed. // // AS/400 Toolbox for Java // (C) Copyright IBM Corp. 1997 // All rights reserved.

32

Getting Started with Java and AS/400: Client/Server Java

// US Government Users Restricted Rights // Use, duplication, or disclosure restricted // by GSA ADP Schedule Contract with IBM Corp. // ////////////////////////////////////////////////////////////////////////////////// import java.io.*; import java.util.*; import com.ibm.as400.access.*; public class CommandCallExample 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 variables 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(); System.out.print(Command: ); commandString = 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 AS/400 system that will // recieve the command. CommandCall command = new CommandCall( as400 ); try { // Run the command. if (command.run(commandString)) System.out.print( Command successful ); else System.out.print( Command failed ); // If messages were produced from the command, print them AS400Message[] messagelist = command.getMessageList(); if (messagelist.length > 0) { System.out.println( , messages from the command: ); System.out.println( ); } for (int i=0; i < messagelist.length; i++) { System.out.print ( messagelist[i].getID() ); System.out.print ( : ); System.out.println( messagelist[i].getText() ); } } catch (Exception e) { System.out.println( Command + command.getCommand() + did not run ); } System.exit(0); } }

Naming your Java application


When naming your Java application, make sure you use the proper naming conventionname the application with the same name as the class you are creating.
Chapter 5. Creating a Java application

33

For example, the Java application listed below creates the class CommandCallExample.

If you look at the example title bar, you will notice its named CommandCallExample.java.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Compiling your Java application


To compile the Java application you wrote, follow these steps: 1. Open an MS-DOS prompt. 2. Navigate to the directory in which you saved your Java application. 3. At the prompt, type: javac MyClass.java where MyClass is the name of your class.
Note: To compile the code example, CommandCallExample, type javac CommandCallExample.java Java is case sensitive. Even though Windows is not, the javac command will fail to compile your code unless your capitalization (case) is consistent.

The title bar of your MS-DOS window changes from MS-DOS Prompt to JAVAC. 4. When the title bar changes back to MS-DOS Prompt and a prompt appears, your Java application has compiled cleanly.
Note: The compiled Java application is saved in the same directory as the source code file. If your source code file is named MyClass.java, the compiled application will be named MyClass.class. [ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

34

Getting Started with Java and AS/400: Client/Server Java

Running your Java application


To 1. 2. 3. run the Java application you have written and compiled, follow these steps: Open an MS-DOS prompt. Navigate to the directory in which you saved your compiled Java application. At the prompt, type: java MyClass where MyClass is the name of your compiled class. Press the Enter key.
Note: If you compiled the code example, CommandCallExample, and would like to run it, type java CommandCallExample. See Running the CommandCallExample for more information about running this Java application.

The title bar of your MS-DOS window changes from MS-DOS Prompt to JAVA. Your Java application will run. 4. After the Java application has finished, an MS-DOS prompt appears, and the title bar of the MS-DOS window changes to MS-DOS Prompt.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Running the CommandCallExample


To run the compiled CommandCallExample, follow these steps: 1. Open an MS-DOS prompt. 2. Navigate to the directory that contains CommandCallExample.class.
Note: This is the directory in which you saved CommandCallExample.java.

3. At the prompt, type: java CommandCallExample and press the Enter key. Notice that the title bar of the MS-DOS prompt window changes to JAVA. 4. You will be prompted to enter your AS/400 system name. Type the name of your system, and press the Enter key. 5. You will be prompted to enter a CL command to run on your AS/400 system. Type any command, like CHGCURLIB CURLIB(QSYS), for example. This command will change the AS/400 systems current library to the QSYS library. Press the Enter key. 6. The Signon to AS/400 window appears. Type your AS/400 user profile name in the User ID blank. Type the password for your profile in the Password blank. Click OK.

Chapter 5. Creating a Java application

35

The signon window disappears. 7. After the command runs, the Java program displays a message about the outcome of the command. For instance, when we ran CHGCURLIB CURLIB(QSYS), we got the following message:
Command successful, messages from the command: CPC2198: Current library changed to QSYS.

8. After the program has successfully run, the MS-DOS prompt appears, and the title bar of the window changes to MS-DOS Prompt.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Examples: Database interaction with Java


The following examples show how Java can be used almost anywhere. Copy, compile, and run the code to see how easy interacting with AS/400 can be. Example:Query a sample table and output its contents (Non-GUI). Example:Query a sample table through a graphical user interface (GUI). To learn more about client/server database applications and Java check out the AS/400 Toolbox for Java JDBC documentation.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Example: AS/400 database interaction through JDBC


////////////////////////////////////////////////////////////////////////////////// // // JDBCQuery example. This program uses the AS/400 JDBC driver to // query a table and output its contents. // // Command syntax: // JDBCQuery system collectionName tableName

36

Getting Started with Java and AS/400: Client/Server Java

// // For example, // JDBCQuery MySystem qiws qcustcdt // ////////////////////////////////////////////////////////////////////////////////// // // This source is an example of AS/400 Toolbox for Java JDBC driver. // IBM grants you a nonexclusive license to use this as an example // from which you can generate similar function tailored to // your own specific needs. // // This sample code is provided by IBM for illustrative purposes // only. These examples have not been thoroughly tested under all // conditions. IBM, therefore, cannot guarantee or imply // reliability, serviceability, or function of these programs. // // All programs contained herein are provided to you "AS IS" // without any warranties of any kind. The implied warranties of // merchantablility and fitness for a particular purpose are // expressly disclaimed. // // AS/400 Toolbox for Java // (C) Copyright IBM Corp. 1997 // All rights reserved. // US Government Users Restricted Rights // Use, duplication, or disclosure restricted // by GSA ADP Schedule Contract with IBM Corp. // ////////////////////////////////////////////////////////////////////////////////// import java.sql.*; public class JDBCQuery { // Format a string so that it has the specified width. private static String format (String s, int width) { String formattedString; // The string is shorter than specified width, // so we need to pad with blanks. if (s.length() < width) { StringBuffer buffer = new StringBuffer (s); for (int i = s.length(); i < width; ++i) buffer.append (" "); formattedString = buffer.toString(); } // Otherwise, we need to truncate the string. else formattedString = s.substring (0, width); return formattedString; } public static void main (String[] parameters) { // Check the input parameters. if (parameters.length != 3) { System.out.println(""); System.out.println("Usage:"); System.out.println(""); System.out.println(" JDBCQuery system collectionName tableName"); System.out.println(""); System.out.println(""); System.out.println("For example:"); System.out.println(""); System.out.println(""); System.out.println(" JDBCQuery mySystem qiws qcustcdt"); System.out.println(""); return; } String system = parameters[0];
Chapter 5. Creating a Java application

37

String collectionName = parameters[1]; String tableName = parameters[2]; Connection connection = null; try { // Load the AS/400 Toolbox for Java JDBC driver. DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); // Get a connection to the database. Since we do not // provide a user id or password, a prompt will appear. connection = DriverManager.getConnection ("jdbc:as400://" + system); DatabaseMetaData dmd = connection.getMetaData (); // Execute the query. Statement select = connection.createStatement (); ResultSet rs = select.executeQuery ("SELECT * FROM " + collectionName + dmd.getCatalogSeparator() + tableName); // Get information about the result set. Set the column // width to whichever is longer: the length of the label // or the length of the data. ResultSetMetaData rsmd = rs.getMetaData (); int columnCount = rsmd.getColumnCount (); String[] columnLabels = new String[columnCount]; int[] columnWidths = new int[columnCount]; for (int i = 1; i <= columnCount; ++i) { columnLabels[i-1] = rsmd.getColumnLabel (i); columnWidths[i-1] = Math.max (columnLabels[i-1].length(), rsmd.getColumnDisplaySize (i)); } // Output the column headings. for (int i = 1; i <= columnCount; ++i) { System.out.print (format (rsmd.getColumnLabel(i), columnWidths[i-1])); System.out.print (" "); } System.out.println (); // Output a dashed line. StringBuffer dashedLine; for (int i = 1; i <= columnCount; ++i) { for (int j = 1; j <= columnWidths[i-1]; ++j) System.out.print ("-"); System.out.print (" "); } System.out.println (); // Iterate throught the rows in the result set and output // the columns for each row. while (rs.next ()) { for (int i = 1; i <= columnCount; ++i) { String value = rs.getString (i); if (rs.wasNull ()) value = "<null>"; System.out.print (format (value, columnWidths[i-1])); System.out.print (" "); } System.out.println (); } } catch (Exception e) { System.out.println (); System.out.println ("ERROR: " + e.getMessage()); } finally { // Clean up. try { if (connection != null) connection.close (); } catch (SQLException e) { // Ignore. }

38

Getting Started with Java and AS/400: Client/Server Java

} System.exit (0);

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

Example: GUI interaction with the AS/400 database


This example creates a simple GUI for collecting input and a database table for presenting output. The input, a last name, is compared to the qcustcdt sample tables LSTNAM schema. Any matching tuples become output. To 1. 2. 3. run this program, do the following: Copy the code below, and save the file as GUIData.java. Compile the code, type: javac GUIData.java. Run the program, type: java GUIData mysytem where mysystem is the name of your AS/400. 4. Because the table is very small, type Jones or Stevens (two names in the table) when instructed. This program requires Java 2 (JDK 1.2 and Swing 1.1). Make sure you read the setting up client/server Java guide beforehand.
////////////////////////////////////////////////////////////////////////////////// // // This program uses the AS/400 JDBC driver to // query a table and output its contents. The Swing classes are used // to create the GUI components. // // Command syntax: // GUIData system // // This program uses the "qiws" (collectionName) and "qcustcdt" (tableName) // ////////////////////////////////////////////////////////////////////////////////// // // This sample code is provided by IBM for illustrative purposes // only. These examples have not been thoroughly tested under all // conditions. IBM, therefore, cannot guarantee or imply // reliability, serviceability, or function of these programs. // // All programs contained herein are provided to you "AS IS" // without any warranties of any kind. The implied warranties of // merchantablility and fitness for a particular purpose are // expressly disclaimed. // ////////////////////////////////////////////////////////////////////////////////// import com.ibm.as400.access.*; import com.ibm.as400.vaccess.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class GUIData { public static void main (String[] parameters) { if (parameters.length != 1) { System.out.println("Usage: GUIData system"); return;
Chapter 5. Creating a Java application

39

} try { DriverManager.registerDriver (new AS400JDBCDriver ()); final SQLConnection connection = new SQLConnection ("jdbc:as400://" + parameters[0]); final SQLResultSetTablePane tablePane = new SQLResultSetTablePane(); tablePane.setConnection(connection); final JFrame g = new JFrame("Client/server JDBC example"); JTextField instructions = new JTextField ("Type a last name (Example: 'Jones' or 'Stevens') and instructions.setEditable(false); final JTextField text_field = new JTextField(20); text_field.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { String query = text_field.getText(); String lname = "SELECT * FROM QIWS.QCUSTCDT WHERE LSTNAM= ( tablePane.setQuery(lname); tablePane.load(); } catch(Exception e) { System.out.println("Error: "+e.getMessage()); } } }); JPanel top = new JPanel(); top.setLayout(new GridLayout(2, 1)); top.add(instructions); top.add(text_field); g.getContentPane().setLayout(new BorderLayout()); g.getContentPane().add("North", top); g.getContentPane().add("Center", tablePane); tablePane.addErrorListener(new ErrorDialogAdapter(g)); g.addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent event) { try { tablePane.close(); connection.close(); } catch(Exception e) {} System.exit (0); } }); g.pack(); g.show(); } catch(Exception e) { System.out.println ("Error: " + e.getMessage ()); System.exit (0); }

[ Information Center Home Page | Feedback ]

[ Legal | AS/400 Glossary ]

40

Getting Started with Java and AS/400: Client/Server Java

Chapter 6. Other information about Java and AS/400


Learn Java! Take a look at some Java education links. And, be sure to look at this list of Java and AS/400 resources which points to books, manuals, websites, magazines, and newsgroups discussing Java on AS/400. Internet applications, Java, and the AS/400 system v Servlets Create servlets and put the web, Java, and AS/400 together. shows how to design v Building AS/400 Internet-Based Applications with Java and build AS/400 Internet- or intranet-based Java applications, applets, and servlets. If you are interested in Java and AS/400s e-business capabilities, this redbook is for you. You will need Adobe Acrobat Reader to view this document. Client/Server Java Links v AS/400 Toolbox for Java product information. This is the online manual for AS/400 Toolbox for Java. Learn about the product features and how to use them. The Tips for Programming section, in particular, provides important tips for writing Java applications that use AS/400 Toolbox for Java classes. Visit this site for information about v AS/400 Toolbox for Java home page . using AS/400 Toolbox for Java, troubleshooting, code updates, and downloads. v (ITSO Redbook) features Building AS/400 Client/Server Applications with Java projects and code examples using AS/400 Toolbox for Java. (You will need Adobe Acrobat Reader to view this document.)

Java on AS/400 Links v AS/400 Developer Kit for Java product documentation. This is the online manual for AS/400 Developer Kit for Java. Learn about the product features and how to use them. v Java for RPG Programmers. Currently, this is the only AS/400-specific introduction to Java. Even if you do not know RPG, you will still find the book invaluable for its description of Java in AS/400 terms.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Getting Started with Java and AS/400: Servlets


Servlets are Java applications that run on and interact with a Java-enabled web server. Just as applets run in a browsers Java virtual machine, servlets run in a web servers Java virtual machine. Servlets perform many of the same functions that CGI (common gateway interface) programs do. However, because servlets can be pre-loaded by the web server, servlets are usually faster than CGI programs that perform the same functions. Servlets are also easier to write and maintain than CGIs, and they are reusable across other server platforms.

Copyright IBM Corp. 1998, 1999

41

The first step to getting started with servlets is to set up your web server with a Java runtime. To do this, install a Java application server on your AS/400. IBM WebSphere Application Server 2.03 for AS/400 comes free with V4R5 OS/400.

Setting up WebSphere Application Server

More servlet resources


v IBM WebSphere Application Server v Servlets: The New 5250 Model

v Introduction to servlets on AS/400 (V4R3) v JavaSoft Servlets . This site is Sun Microsystems home for Java Servlets. Check out the Servlet API (applied programming interface) and links to other non-platform specific servlets resources. is an on-line Server-side Java Magazine which features v Servlet Central articles about servlet development. It has an extensive Resources section with links to other servlet information. v WebSphere Application Server PTFs Lists the PTFs needed to ensure Websphere runs correctly.
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Java and AS/400 resources


If you find a helpful Java and AS/400 source on the Internet that is not listed here, send us your comments. v Books (page 42) v Product documentation (manuals) v ITSO Redbooks v Magazines (page 43) v Internet sites (page 43) v Newsgroups, forums, and mailing lists
Note: See the Java education page for information about learning the Java programming language.

Books by Phil Coulthard and George Farr of IBM v Java for RPG Programmers Toronto. Currently the only available AS/400-specific guide to Java, this book is extremely helpful for Java programming on AS/400. Also includes a MINQ Java tutorial CD-ROM, Introduction to Java Programming using VisualAge for Java Enterprise. Product documentation (manuals) v AS/400 Developer Kit for Java

42

Getting Started with Java and AS/400: Client/Server Java

v AS/400 Toolbox for Java v Qshell Interpreter ITSO Redbooks

ITSO Redbooks v Building AS/400 Applications with Java applications to Java. has examples of migrating RPG

v Building AS/400 Client/Server Applications with Java code examples using AS/400 Toolbox for Java.
Note: You will need Adobe Acrobat Reader

features projects and

to view this document.

Building AS/400 Internet-Based Applications with Java shows how to design and build AS/400 Internet- or intranet-based Java applications, applets, and servlets. If you are interested in Java and AS/400s e-business capabilities, this redbook is for you.
to view this document.

Note: You will need Adobe Acrobat Reader

Magazines v NEWS/400 magazine offers several good AS/400 and Java resources in its

such as The Java Tutorial, Java articles archive, and a Java Java Community newsletter called Java Enterprise Solutions. v Midrange Computing magazine . Since MC does not publish many of their articles on the Internet, you will probably want to get a subscription for the good Java articles they frequently feature. You can also browse through and order back issues for any good articles you might have missed. MC also publishes the AS/400 NetJava Expert Internet sites features white papers on AS/400 and Java, v AS/400 and Java home page providing links to other Java resources. View all sorts of information about v AS/400 Toolbox for Java home page. AS/400 Toolbox for Java, including evaluation downloads, troubleshooting, and frequently-asked questions. v Qshell Interpreter home page provides references, downloads, and frequently-asked questions about the Qshell Interpreter licensed program. newsletter.

Chapter 6. Other information about Java and AS/400

43

The most comprehensive site v AS/400 Partners in Development Java page. dedicated to Java and AS/400, this home page features articles, demonstrations, solutions, resources, and information about real world Java implementations by AS/400 business partners. Visit this site from IBM v IBM Centre for Java Technology Development. Hursley for Java news, information, free code downloads, and environment support for all IBM platforms. Subtitled The Ultimate Resources for Java v IBM Java home page. Developers, IBMs main Java site offers developer tools and assistance, technology previews, Java tutorials and education, feature articles, and JCentral, a search engine tuned to Java on the Internet. v IBM SanFrancisco home page. IBM SanFrancisco is a Java-based collection of business application components that allows you to assemble server-side applications from pre-existing parts. The SanFrancisco home page provides information and resources about the project. If you are interested in what an v VisualAge for Java and AS/400 home page. integrated development environment (IDE) like VisualAge for Java can do for your Java and AS/400 programming, see this site. Newsgroups, forums, and mailing lists Newsgroups v IBM Hursley AS/400 Java Environment newsgroup v AS/400 newsgroup Forums v IBM Hursley AS/400 Java Environment forum v Midrange Computing Java Programming Discussion forum v News/400 Java forum Mailing lists v MIDRANGE dot COM JAVA/400 mailing list
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Java education
Learning Java does not have to be expensive. You can find several good Java tutorials on the Internet as well as other materials like books and articles. Listed below are some good resources for learning about Java programming. See both 44 and 45 links.

44

Getting Started with Java and AS/400: Client/Server Java

See Java and AS/400 resources for other links that will be helpful in your Java education. AS/400-specific Java education features AS/400-specific Java v AS/400 Partners in Developer Java Education education such as books, Internet-based courses, classroom training, and workshops. v This course is part of the IBM Education and Training AS/400 Curriculum. v NEWS/400s Java Tutorial features a collection of articles written by Java and AS/400 experts Phil Coulthard and George Farr. v Java for RPG Programmers by Phil Coulthard and George Farr of IBM Toronto. Currently the only available AS/400-specific guide to Java, this book is extremely helpful for learning Java programming for AS/400. The book also includes a MINQ Java tutorial CD-ROM, Introduction to Java Programming using VisualAge for Java Enterprise. General Java education includes online courses, classroom training, white v IBM Java Education papers, tutorials, articles, books, and FAQs. v The Java Tutorial lists online Java tutorials (basic and advanced Java topics) available from Sun Microsystems, Inc. walks through how to use the v Java Programming Language Basics, Part 1 Java 2 Platform software to create and run three common types of programs written for the Java platformapplications, applets, and servlets. (Sun Microsystems, Inc.)
[ Information Center Home Page | Feedback ] [ Legal | AS/400 Glossary ]

Chapter 6. Other information about Java and AS/400

45

46

Getting Started with Java and AS/400: Client/Server Java

Printed in U.S.A.

You might also like