C H A P T E R  8

Tutorial--Section 2.2
Create the CustomerModel

This chapter describes how to create a model to access the RDBMS in the Suntrademark ONE Application Framework application.


Task 2: Create the CustomerMode

Create a JDBCtrademark SQL Model

1. Select the main module folder.

2. Click the Add Model button on the Sun ONE Application Framework toolbar.

This figure shows the Add Model button on the Sun ONE Application Framework toolbar. 

The Select Model Type panel displays.

This figure shows the Select Model Type panel. 

3. Enter CustomerModel in the Name field.

4. Select JDBC SQL Query Model from the model component list.

The list you see might vary depending on the Sun ONE Application Framework version and the possible addition of custom or third party component libraries.

5. Click Next.

The Select Datasource page displays.

This figure shows the SElect Datasource page. 

6. Select jdbc/jdbc-pointbase from the combo box.

7. Click Next.

The Select Database Tables page displays.

This figure shows the Select Database Tables page. 

8. Select CUSTOMER_TBL.

9. Click Add.

10. Click Next.

The Select Table Columns page displays.

This figure shows the Select Columns page. 

11. Click Add All to include all of the columns in your Model.

12. Click Finish to create the Model.

The CustomerModel object is created in the main module.

 The figure on the left shows the CustomerModel object in the main module. The figure on the right shows the code that was created.

13. Expand the CustomerModel to see all of the columns.

14. Double-click the CustomerModel folder to view the code in the CustomerModel Java class.

Mark the Model's Key Field(s)



Note - Due to a special type of key field indicator in the PointBase database schema metadata, the Model wizard does not properly detect the key field CUSTOMER_TBL_CUSTOMER_NUM. Therefore, you must set the key field manually.

This is not a problem if you create the datasource from a database schema object, and is also not a problem for non-PointBase databases, such as Oracle.



1. Under the Columns node of the CustomerModel, select the CUSTOMER_TBL_CUSTOMER_NUM model field.

2. In the property sheet, select the Model Field Properties tab.

If the Properties tab is not visible, click the View -> Properties menu option, or right-click the key field column and select the Properties action.

3. Change the value of the Key Field property from false to true.

This figure shows the Key Field property (selection is True). 

Add Connection Code for Non-JNDI Enabled Containers

For servlet containers that do not support JNDI data sources, you can rely on explicit use of a JDBC driver.



Note - In section 2.1 of this tutorial (Task 1: Accessing a SQL Database), if you are testing your this application in a servlet container that does not support JNDI, you disabled the use of JNDI and declared the explicit use of the PointBase JDBC driver in the SQLConnectionManagerImpl class.



If you are testing in a servlet container that does not support JNDI, you must set the connection username and password explicitly in the model so that a proper database connection can be created before the model is executed.



Note - For production environments, you should use JNDI connections.



Add the bold code below to the CustomerModel's constructor.

public CustomerModel() 
{ 
        super();
       setDefaultConnectionUser("pbpublic");
       setDefaultConnectionPassword("pbpublic");
	} 

While providing the datasource username and password as demonstrated above is not a good practice for a real world application, it is practical for this tutorial. Take extra care to obtain and provide the username and password in a more secure and robust implementation. When using the JNDI method, this code is unnecessary and this login information is provided by the configured JNDI connections in the application server.