Professional Documents
Culture Documents
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
27 28 29
31
31 32 33 34 35 35 36 36 39
13
. 13 . 14 . 14 16 . 17 . 19 . 19 . 20
iii
iv
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.
V4R3
SF99066
V4R4
SF99067
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 ]
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 ]
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 ]
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 ]
10
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 ]
11
12
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 ]
13
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 ]
14
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.
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 ]
16
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.
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.
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 ]
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
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 ]
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 ]
20
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.
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
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
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 ]
25
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 ]
26
;%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.
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
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.
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
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.
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
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 ]
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 (*.*).
32
// 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); } }
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 ]
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
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 ]
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.
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 ]
36
// // 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
} System.exit (0);
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); }
40
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 ]
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.
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 ]
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
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
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.
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.
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
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 ]
45
46
Printed in U.S.A.