Application Developer provides features to perform the following development tasks: ? Creating a Java project ? Creating Java packages ? Running your programs ? Exporting code and runnin
Trang 1Creating and working with a Java project
Before we demonstrate Application Developer’s programming assists, we show how to create and work with a Java project You can use Application Developer
to develop the Java packages for your application specific business logic and for common code that will be used by several projects These packages can be exported as JAR files and included in the build path for other types of projects, or
in the application path of the application servers
Existing Java code can also be imported and integrated into new applications Application Developer 5 also introduces the Visual Editor for Java, which allows you to design applications containing a graphical user interface (GUI) See Chapter 14, “Developing GUI applications” on page 469 for a detailed description
of the Visual Editor
Application Developer provides features to perform the following development tasks:
Creating a Java project
Creating Java packages
Running your programs
Exporting code and running outside Application Developer
Locating compile errors in your code
Debugging your code
Creating a Java project
Java projects contain the resources needed for Java applications, including Java files and class files Java projects are used to create Java packages When you create a new Java project, the environment is set up for Java development
To create a new Java project, select File -> New -> Project This displays the New Project dialog (Figure 5-1)
Trang 2Figure 5-1 New Project dialog
Select Java and Java Project from this dialog and click Next to start the Java project wizard (Figure 5-2)
Figure 5-2 New Java project: project name and directory
On the first page you name the Java project and specify the directory, where the project files should be stored In this example the project is named
ItsoProGuideJava By default, the project files will be stored in a directory created under the Application Developer workspace directory You can change the project directory by removing the Use default check box and specifying another directory
Trang 3If you click Finish, the project is configured with default options that can be configured in the preferences (select Window -> Preferences and then select
Java -> New Project to set up defaults for a new Java project)
We click Next to bring up the second dialog of the wizard, where we define the Java build settings for the new project (Figure 5-3)
Figure 5-3 New Java project: source settings
On the Source tab you decide whether it is appropriate to store source code directly in the project folder, or if you want to use separate source folders For our sample project, the simple model is used If you want to use the complex model instead, you can create the required folders by clicking Create New Folder and adding them to the list
Here you can also select the target folder for the generated class files By default the class files are placed in the same folder as the source folder, but you can edit the Build output folder field to define a different target folder
Note: In the Package Explorer view you cannot see the generated class
files If you open the Navigator view (Window -> Show View -> Navigator) you can see source and class files
Trang 4On the Projects tab you can specify any other projects in your workspace that should be in the Java build path for the new project (Figure 5-4) You might have some common code in a project that already exists and you want to reuse it in the new project
Figure 5-4 New Java project: required projects
On the Libraries tab, you can add other code libraries that have to be included in the build path of your project (Figure 5-5) By default, the library list contains an entry representing the Java runtime library
You can also add:
Workbench-managed (internal) JAR files
File system (external) JAR files
Folders containing class files
You can also add Java classpath variables which have been defined in the Classpath Variable preferences page Consult the section “Java classpath variables” on page 33 for more information about the Classpath Variable
preferences page
The Add Variable button allows you to add classpath variables to the build path
Classpath variables are symbolic pointers to JAR files with the benefit of
avoiding local file system paths in a classpath
Trang 5This is a good idea when projects are shared in a team Variables can be created and edited in the Classpath Variable preference page If you want to open the Classpath Variable preferences page, click Window -> Preferences -> Java -> Classpath Variables
Figure 5-5 New Java project: libraries settings
On the last tab, Order and Export, you can specify the order in which you want items in the build path to be searched Using the Up and Down buttons allow you
to arrange the order of the classpath entries in the list (Figure 5-6)
The checked list entries are marked as exported Exported entries are visible to projects that require the project Use the Select All and Deselect All buttons to change the checked state of all entries The source folder itself is always exported, and cannot be deselected
Clicking Finish creates the new project
Trang 6Figure 5-6 New Java project: order and export settings
Creating Java packages
Once the Java project has been created, you can add Java packages to it Select the project in the Package Explorer view and New -> Package from the context menu In the dialog window, you enter the fully qualified name of the package The package where our sample Java application code resides is named itso.java (Figure 5-7)
Figure 5-7 Create Java package dialog
Trang 7Creating Java classes
After you have created the new package, you can add classes to it The sample code is in a new class called CustomerListing To create a new class, select the package that has been created in the previous step and select New -> Class from the context menu In the Name field of the new Java class dialog, you have to enter the name of the new class, CustomerListing (Figure 5-8)
You can also set the modifiers, the name of the superclass, add interfaces which should be implemented, and create method stubs for the new class
Figure 5-8 Create Java Class dialog After all settings have been made, you click Finish to create the class The Java editor opens with the new class and its selected method stubs (Figure 5-9)
Trang 8Figure 5-9 Java source editor with new class
CustomerListing sample
Complete the CustomerListing class with the sample code from:
\SG246957\sampcode\dev-java\CustomerListing.java
We do not explain in detail the code for the CustomerListing class The basic functions of the sample code are:
Connect to the DB2 database
Select all customers from the CUSTOMER table
Display the first name, the last name and the user ID of the customers Chapter 6, “Developing database applications” on page 139 describes database access using JDBC For now we just use this class to experiment with editing, running, and debugging a Java class
Figure 5-10 shows the complete code of the sample CustomerListing class
Note We also provide a companion program, CustomerListingNet, which uses
the DB2 JDBC net driver to connect to the database:
Class.forName("COM.ibm.db2.net.DB2Driver");
con = DriverManager.getConnection("jdbc:db2://localhost:6789/EJBBANK");
Tip: Notice the Javadoc comment that is generated for a new class You can
tailor the comment by selecting Window -> Preferences -> Java -> Templates
and editing the typecomment entry
Trang 9Figure 5-10 Sample class code CustomerListing
package itso.java;
import java.sql.*;
public class CustomerListing {
static String dbtab = "CUSTOMER";
public static void main(String[] args) {
System.out.println("Customer Listing for " + dbtab);
Connection con = null;
con = connect();
Statement stmt = null;
ResultSet rs = null;
String select = "SELECT * FROM ITSO." + dbtab;
try { stmt = con.createStatement();
rs = stmt.executeQuery(select);
while (rs.next()) { String firstName = rs.getString("firstName");
String lastName = rs.getString("lastName");
String userID = rs.getString("userID");
System.out.println(firstName + " " + lastName + " " + userID); }
System.out.println("End of Listing");
} catch (Exception e) { System.err.println("Exception: " + e.getMessage());
} finally { try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException e) {}
} }
protected static Connection connect() {
Connection con = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
con = DriverManager.getConnection("jdbc:db2:EJBBANK");
} catch (Exception e) { System.err.println("Exception: " + e.getMessage());
} return con;
} }
Trang 10Running your programs
After the code has been completed and is free of compile errors, it can be executed using the Workbench Java Application Launcher To launch the program, you click the Run icon from the toolbar
If you launch the program the first time, the Launch Configuration dialog opens (Figure 5-11) Here you can select the type of configuration you would like to create to run the program
Select Java Application from the list and click New A new launch configuration with the appropriate settings for the selected class is created You can also specify arguments, JRE, and classpath settings for your launch configuration Clicking Run invokes the main method of the class
Figure 5-11 Launch Configurations dialog
You can also use the drop-down arrow of the Run icon Clicking this drop-down arrow the first time allows you either to open the configuration launcher or select the type of application you would like to run, directly
You can add other launch configurations for any program Each configuration is displayed when clicking the drop-down arrow of the Run icon
Tip: By defining launch configurations for a program you can create multiple
configurations with different arguments and settings (such as the JRE)