WLSKB004 Accessing Database in WebLogic Workshop Using the RowSet ControlBibhas Bhattacharya, Web Age Solutions Inc. IntroductionWebLogic Workshop provides two different controls to facilitate database access.
The RowSet approach has a few advantages.
In this article we will develop an application that implements the full Create Retrieve Update and Delete (CRUD) cycle. Before You Get StartedThis is an advanced tutorial. It assumes that you know how to work with Page Flow. If you are new to WebLogic Workshop, please complete the following tutorials before starting with this tutorial.
Create a new application called BasicApp unless it is already created from a previous tutorial. The Database SchemaWebLogic Workshop comes with a sample PointBase database called WORKSHOP. A data source called cgDataSource is also defined in the WebLogic Server for that database. We will use that database (as opposed to setting up a new one) to avoid getting distracted by the WebLogic and PointBase administration activities. In this step, we will get familiar with the ITEMS table in the WORKSHOP database. This table contains a lits of products. Our application will manage the products in this table. First, launch WebLogic Workshop. Start the WebLogic Server (Tools->WebLogic Server->Start WebLogic Server). This will also start the PointBase database server. The database server is configured to listen on port 9093. Open a command window. Change directory to c:\bea\weblogic81\common\eval\pointbase\tools. Run the startPointBaseConsole.cmd command.
In the Connect To Database dialog, enter the following values. URL: jdbc:pointbase:server://localhost:9093/workshop Click on OK.
Expand SCHEMAS->WEBLOGIC->TABLES. Scroll down and locate the ITEMS table.
Expand ITEMS->COLUMNS. Expand each column and note the data type. Right click on ITEMS and select SELECT * FROM "WEBLOGIC"."ITEMS". Make sure that you can see the sample data.
Create the Page FlowIn WebLogic Workshop, create a new application called BasicApp (unless it's already created from a previous tutorial).
Right click on BasicAppWeb and select New->Page Flow. Enter ItemManagement as the name of the Page Flow. Click on Next. Click on Create. Add the RowSet ControlIn the Application view, right click on the ItemManagement folder and select New->Other File Types. Select Business Logic and then RowSet Control.
Click on Create.
Set the name of the control to ItemDB. Make sure that the cgDataSource data source is selected. Click on Next.
Enter the following values. Select The Methods To Create: Choose the Query and update a database table radio box. Click on Next. Accept the default. Click on Next again.
Accept the default again. Click on Create. System will create a control extension class called ItemDB.jcx. Now, we must add the control to our Page Flow. Double click on ItemManagementController.jpf to open the Page Flow in the diagram editor. Click on the Action View tab at the bottom of the editor. Drag and drop ItemDB.jcx on to the editor.
Display Item ListIn this step, we will display all the products in the ITEMS table. Click on the Flow View tab at the bottom of the Page Flow editor. Right click on the Page Flow diagram and select New Action.
Enter listItems as the name of the new action. Select the No Form Bean radio button. Click on OK. Right click on the diagram and select New Page. Change the name of the page to list_items.jsp. Draw an arrow from the listItems action to list_items.jsp. Right click on index.jsp and select Delete to delete it. Draw an arrow from the begin action to the listItems action.
Click on the Source View tab at the bottom of the diagram editor. Add the following import statements to the Page Flow class. import javax.sql.RowSet; import com.bea.control.DatabaseFilter; Scroll down and locate the control variable. /** * @common:control */ private ItemManagement.ItemDB itemDB; Note: ItemDB is an interface. An implementation class is generated behind the scene. System also instantiates an object of that class and initializes the itemDB member variable. Scroll down further and locate the listItems() method. Change the method as follows. protected Forward listItems() throws Exception Here, we obtain all rows in the ITEMS table as a RowSet object. We save the RowSet object in the request scope using the name itemList. Save changes (Control+S). Now, we will need to work on the list_items.jsp page. Open the JSP file in the editor. Set the contents of the <body> tag to as follows. <body>
<h2>Item List</h2>
<p>
<netui:anchor href="add_item_form.jsp">Add Item</netui:anchor>
</p>
<netui-data:repeater dataSource="{request.itemList}">
Here, we use the <netui-data:repeater> tag to show the items in a table. Note, how each column value is shown (as in {container.item.ITEMNUMBER}). Save changes (Control+S). TestOpen a web browser and enter the URL: http://localhost:7001/BasicAppWeb/ItemManagement/begin.do.
Create a Form BeanBefore we can implement the edit and add feature, we must create a form bean class. Open the Page Flow diagram editor form the ItemManagementController Page Flow. Create a new form bean called ItemFormBean with the following properties.
Click on OK. Save changes. Add Edit FeatureAdd a new action to the Page Flow called displayEditForm. This action does not require any form bean. Add a page called edit_item_form.jsp. Draw an arrow from displayEditForm to edit_item_form.jsp.
Add another action called editItem with the ItemFormBean associated with it.
Draw an arrow from the editItem action to listItems.
Set the code for the displayEditForm action to as follows. protected Forward displayEditForm() throws Exception
{
RowSet rs = itemDB.detailsItems(new Integer(getRequest().getParameter("itemNumber")));
if (rs.next()) {
ItemFormBean ib = new ItemFormBean();
ib.setItemName(rs.getString("itemName"));
ib.setItemNumber(rs.getInt("itemNumber"));
ib.setQuantityAvailable(rs.getInt("quantityAvailable"));
ib.setPrice(rs.getDouble("price"));
getRequest().setAttribute("itemFormBean", ib);
}
return new Forward("success");
}
Note: It is important that we save the form bean in the request scope using a form bean's name. Set the contents of the <body> tag of the edit_item_form.jsp to as follows. <body>
<netui:form action="editItem">
<p>Edit item number: <netui:label value="{actionForm.itemNumber}"/></p>
Change the implementation of the editItem action method as follows. protected Forward editItem(ItemFormBean form) throws Exception
{
RowSet rs = itemDB.detailsItems(new Integer(form.getItemNumber()));
if (rs.next()) {
rs.updateString("itemName", form.getItemName());
rs.updateInt("quantityAvailable", form.getQuantityAvailable());
rs.updateDouble("price", form.getPrice());
rs.updateRow();
itemDB.updateItems(rs);
}
return new Forward("success");
}
Save all files. Add the Create FeatureAdd a new action called addItem with the ItemFormBean associated with it. Draw an arrow from the action to the listItems action. Set the implementation code for the addItem action to as follows. protected Forward addItem(ItemFormBean form) throws Exception
{
RowSet rs = itemDB.detailsItemsTemplate();
rs.updateString("itemName", form.getItemName());
rs.updateInt("quantityAvailable", form.getQuantityAvailable());
rs.updateDouble("price", form.getPrice());
rs.insertRow();
itemDB.insertItems(rs);
return new Forward("success");
}
Create a JSP page called add_item_form.jsp. Set the contents of the <body> tag as follows. <body>
<p>Add Item</p>
<netui:form action="addItem">
Name: <netui:textBox dataSource="{actionForm.itemName}"/><br>
Price: <netui:textBox dataSource="{actionForm.price}"/><br>
Quantity Available: <netui:textBox dataSource="{actionForm.quantityAvailable}"/><br>
<netui:button value="Update" type="submit"/>
</netui:form>
</body>
Save changes. Add the Delete FeatureAdd a new action called deleteItem. It does not require a form bean. Draw an arrow from the action to the listItems action. Set the code for the deleteItem action as follows. protected Forward deleteItem() throws Exception
{
RowSet rs = itemDB.detailsItems(new Integer(getRequest().getParameter("itemNumber")));
if (rs.next()) {
rs.deleteRow();
itemDB.deleteItems(rs);
}
return new Forward("success");
}
Create LinksOpen list_items.jsp. Locate the line. <td bgcolor="white">Edit Delete </td> Change it to: <td bgcolor="white">
<netui:anchor action="displayEditForm">
Edit
<netui:parameter name="itemNumber" value="{container.item.ITEMNUMBER}"/>
</netui:anchor>
<netui:anchor action="deleteItem">
Delete
<netui:parameter name="itemNumber" value="{container.item.ITEMNUMBER}"/>
</netui:anchor>
</td>
Save changes. TestOpen a browser. Enter the URL: http://localhost:7001/BasicAppWeb/ItemManagement/begin.do. Make sure that you can add, edit and delete items. Feedback |