Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Hello World: Rational Application Developer

Access a Cloudscape database without using JDBC

Jane Fung (mailto:jcyfung@ca.ibm.com), Software Developer, IBM Toronto Lab
Jane Fung works on the WebSphere Studio Application Developer Integration Edition tools team. Jane earned a Bachelor of Applied Science degree in Electrical Engineering at the University of Waterloo, Ontario, and is a Sun Java 2 Certified Programmer. You can reach Jane at jcyfung@ca.ibm.com .

Summary:  Welcome to the second tutorial in the "Hello, World" series, which provides high-level overviews of IBM software products. This tutorial introduces you to IBM Rational Application Developer and highlights some of its basic features. It includes practical exercises that show how to create a Java application, create and invoke a Web service, and create a Web application that can access a Cloudscape database using a Relational Record List.

View more content in this series

Date:  13 Jun 2006
Level:  Introductory PDF:  A4 and Letter (2125 KB | 38 pages)Get Adobe® Reader®

Activity:  28092 views
Comments:  

Creating a Web application that accesses a database

In the final exercise, create a Web application that can access a Cloudscape database using a Relational Record List. First create a Web project, and then later add the database display capability to the Web application.

Would you like to see these steps demonstrated for you?

Show me Show me

Creating a new Web project

Web application development can be done in the Web perspective. All Web elements, such as JSP pages and Java servlets, are stored in Web projects.

A J2EE application consists of several components: Application Client, Web, and Enterprise JavaBeans (EJBs) modules. Each module is a self-contained unit that can be deployed and run on a server individually or as a group in an enterprise application. You work with a Web module in this exercise.

  1. From the workbench, select File > New > Project.
  2. Select Web > Dynamic Web Project > Next.
  3. Enter MyWebProject as the project name. The Show Advanced button shows the advanced settings. Click it to display the default enterprise application project named MyWebProjectEAR. Click Finish.
  4. When you are asked to switch to the Web Perspective, click Yes.

Creating a JSP file

The major contents of a Web project are JSP files and servlets. Other Web elements, such as HTML, style sheets, and images, are also stored in a Web project. Figure 14 shows the structure of a Web project, with its different Web elements in different locations.

After you create a Web project, you can see it in the Dynamic Web Projects folder. The Web project is set up in compliance with the J2EE standard to be exported as a WAR file for deployment. Java servlets and other supporting Java classes are stored in the JavaSource folder inside the Java Resources directory. JSP files, HTML pages, style sheets, Java script files, and images are stored in the WebContent directory. The Web Deployment Descriptor, web.xml, contains the deployment configurations of the Web application. It must be present in the Web module to be deployed on a J2EE server, such as WebSphere Application Server.


Figure 14. Structure of a Web project
Figure 14: Structure of a Web Project
  1. Right-click on MyWebProject > New > JSP File.
  2. Enter MyFirstJSP as the file name and click Finish. Notice that the JSP file is created under the folder WebContent.
  3. Modify the JSP file from the editor using the Design or the Source view. Switch to the Source view.
  4. Enter the bold code in the editor. This line is called a scriptlet, which is Java code enclosed by <% and %>. You can enter any Java code within these brackets. You can also use scriptlets to print something in a JSP file. For example, <%="hello"%> prints the text hello.

    Listing 4. Code for MyWebServiceClient.jsp
                                
    <BODY>
    <P>Place content here.</P>
    <%
    String a = "Hello";
    a+= "World!!";
     %>
    <%=new java.util.Date()  %>
    <%=a %>
    <BR>
    </BODY>

  5. Press Ctrl-S to save.

Running the JSP file in a browser

With Application Developer you can run the JSP file in the embedded server. Start the server from the Server view and then run the JSP file using the URL http://localhost:9080/MyWebProject/MyFirstJSP.jsp. Alternatively, right-click the JSP file and select Run on Server.

  1. In the Project Explorer view, right-click the MyFirstJSP.jsp file and select Run > Run on Server.
  2. Select an existing server and click Finish to automatically add the project to the server and run the JSP file in the internal browser:


    Figure 16. Running the MyFirstJSP
    Figure 16: Running the MyFirstJSP


Creating a database connection

Application Developer has a number of relational database tools, or views, that can be accessed through the Data perspective. Some of the more important views in this perspective are the Data Definition, Database Explorer, and DB Output views.

In the Database Explorer view, connections can be made to a list of supported relational databases. Some of the supported databases are IBM Cloudscape, IBM DB2 Universal Database, Microsoft SQL Server, Sybase Enterprise Systems, and Oracle Database. Refer to the product help for a complete list of the supported databases.

In this step, create a database connection to a Cloudscape database that is shipped with Application Developer. Cloudscape database can use any file system folder as a database. You do not need to create the database before trying to establish a connection. If you select Create the database if required, the Cloudscape database is created before the connection is established.

Cloudscape accepts only one database connection at a time. For example, if a WebSphere Application Server is already connected to the Cloudscape database, an attempt to make a connection from the Database Explorer will fail.

Following are the steps to create a Cloudscape connection:

  1. If the server is running, stop it now or you cannot establish a database connection. In the Servers view, right-click on WebSphere Application Server v6.0 and select Stop.
  2. Switch to the Data perspective. Select Window > Open Perspective > Other. Select the Show all check box and select Data. Click OK. Select OK if asked to enable the Core Database Development capability.
  3. Right-click Database Explorer, located in the bottom left of the perspective and click New Connection.
  4. Click Choose a database manager and JDBC driver. Enter Con1 as the connection name. Click Next.
  5. The database manager defaults to Cloudscape V5.1 as shown in Figure 17. Enter any file system directory in the Database location, for example, C:\HelloWorldSeries\AddressDB. You do not have to physically create the folder in the file system. Just type in a new folder name. Make sure the Create the database if required box is checked. If it is, the tool sets up the Cloudscape database at that location. Because Cloudscape is a test database, you do not need a user ID and password to connect to it. Enter any user ID in the user information section.
  6. Click Test Connection to see if a connection can be established. You should see a window saying the connection is successful. Click Finish to create the connection. If it returns an error stating the user name is invalid, leave both user ID and password blank.
  7. Click No when asked to copy database metadata to a project folder.


    Figure 17. Creating a Cloudscape database connection
    Figure 17: Create a Cloudscape database connection

  8. Con1 displays in the Database Explorer view. Expand the connection to see AddressDB as shown in Figure 18:


    Figure 18. Database Explorer Con1 connection
    Figure 18: Database Explorer Con1 connection


Creating a database definition

Right now, the database is empty. Use the Data Definition view to create a schema and table definition. The database definition is something you define in the workspace which doesn't exist in the database. Later in this hands-on, deploy the definitions to an actual database.

  1. In the Data perspective's Data Definition view, right-click MyWebProject and click New > Database Definition:


    Figure 19. Creating a database definition
    Figure 19: Create Database Definition

  2. Enter AddressDB as the database name. Select Cloudscape V5.1 as Database vendor type, and click Finish.
  3. Expand MyWebProject. Right-click ADDRESSDB and click New > Schema Definition:


    Figure 20. Creating a schema definition
    Figure 20: Create Schema Definition

  4. Accept all the defaults in the schema definition window. Click Finish.
  5. Create a table definition named AddressTable. Right-click on the newly created SCHEMA1 > New > Table Definition:


    Figure 21. Creating a table definition
    Figure 21: Create Table Definition

  6. Enter AddressTable as the table name, and click Next.
  7. Click Add Another to add a column. Enter id as the column name. Select INTEGER as the column type and click the Key column check box.
  8. Click Add Another. Enter name as the column name. Select VARCHAR as the column type. Change the string length to 100.
  9. Click Add Another. Enter address as the column name. Select VARCHAR as the column type. Change the string length to 200.
  10. Click Finish.


    Figure 22. Table columns for AddressTable
    Figure 22: Table Columns for AddressTable

  11. In the Data Definition view, right-click the newly created SCHEMA1.ADDRESSTABLE and click Deploy. Click Next, and then click Next again. This creates SCHEMA1 and AddressTable in the Cloudscape database:


    Figure 23. Deploy the schema definition
    Figure 23: Deploy the schema definition

  12. Click the Use existing connection check box. Select Con1 as the database connection. Click Finish.
  13. In the Database Explorer, right-click your connection Con1 and click Refresh:


    Figure 24. Refresh on the database connection
    Figure 24: Refresh on the database connection

  14. Expand your connection until you see the Tables folder. You see the schema and AddressTable you just created:


    Figure 25. After the schema definition is deployed
    Figure 25: After the schema definition is deployed


Creating an insert statement

Although you have created the table in the database, it has no data. Create an Insert statement that can insert a record into the table.

  1. In the Data Definition view, expand MyWebProject > ADDRESSDB. Right-click Statements and click New > Insert Statement:


    Figure 26. Creating an insert statement
    Figure 26: Create an Insert Statement

  2. Enter CreateRecord as the name, and click OK.
  3. Right-click in the middle panel labeled Tables and click Add Table. Select SCHEMA1.ADDRESSTABLE as the table name. Click OK.
  4. Click all three check boxes in the AddressTable to select all the columns in AddressTable.
  5. In the bottom panel of the editor, enter 1 for the ID column value. Enter 'Jane' as the name value, '8200 Warden Ave' as the address value.
  6. Save the file and close the editor.


    Figure 27. Insert Statement editor
    Figure 27: Insert Statement editor

  7. In the Data Definition view, right-click CreateRecord and click Execute. Select Use an existing connection and Con1. Click Next and Finish. If you get an error stating "Array index out of range: 0", use a new connection instead of reusing an existing connection when executing the statement.


    Figure 28. Execute CreateRecord
    Figure 28: Execute CreateRecord

  8. Go to the Database Explorer view. Right-click the connection Con1 and click Refresh.
  9. Expand AddressDB in the Database Explorer view until you see SCHEMA1.ADDRESSTABLE. Right-click AddressTable and click Sample Contents:


    Figure 29. Sample contents
    Figure 29: Sample contents




    Figure 30. Sample contents results
    Figure 30: Sample contents Results


Modifying the JSP file to display database content

In this step, add a Relational Record List to the MyFirstJSP.jsp to display the table content. A Relational Record List lets you hook up to a database without doing any JDBC coding. Drag and drop the record list to the JSP file to make a read-only list.

  1. Switch to the Web perspective and open MyFirstJSP.jsp in the editor.
  2. In the editor, switch to the Design view. From the Palette, click on Relational Record List and drop it to the bottom of the JSP editor's canvas. Use the Relational Record List to make multiple rows of data available to the page, for example all the rows from a table.


    Figure 31. Relational Record List
    Figure 31: Relational Record List

  3. Enter addressTableRecordList as the Name. Click Next.
  4. Click on New to create a new connection.
  5. In the New Connection window, you should see the existing Con1 connection. Select AddressDB from Con1 as shown in Figure 32. Click Finish:


    Figure 32. Create a new connection in the Relational Record List
    Figure 32: Create a new connection in the Relational Record List

  6. Select SCHEMA1.ADDRESSTABLE and click Finish. A table that displays the database rows is created on the JSP page.


    Figure 33. Select a database row for the Relational Record List
    Figure 33: Select a database row for the Relational Record List

  7. Save the file. Figure 34 shows the final JSP page:


    Figure 34. JSP editor with the Relational Record List
    Figure 34: JSP Editor with the Relational Record List


Running the JSP file in a browser

Start the server and run the JSP file.

  1. In the Server view, right-click on WebSphere Application Server v6.0 and select Start.
  2. Right-click on WebSphere Application Server v6.0 > Restart Project > MyWebProjectEAR. Because you have modified the project, restart the EAR to pick up the changes. This prevents the occurrence of a NullPointerException error when you run the JSP file. If the error still persists, add or remove projects from the server. Right-click on the server > Add or remove projects, select MyWebProjectEAR and remove it from the right pane. Click Finish, then repeat the actions to add MyWebProjectEAR to the right pane.
  3. Wait until the publishing is finished before running the JSP file. In the Project Explorer view, right-click MyFirstJSP.jsp and select Run > Run on Server. Click Finish.
  4. The JSP displays the row from the AddressTable. Enter more rows to the AddressTable and run the JSP file again. You should see more rows in the JSP.


    Figure 35. Running the JSP file
    Figure 35: Run the JSP file

6 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, Java technology
ArticleID=127351
TutorialTitle=Hello World: Rational Application Developer
publish-date=06132006
author1-email=mailto:jcyfung@ca.ibm.com
author1-email-cc=