C H A P T E R 7 |
Tutorial--Section 2.1 Prepare Application to Access SQL Database |
This chapter describes how to expand the application and prepare the Sun ONE Application Framework application to access a SQL Database.
Expand the existing application by adding a SQL-based model and a page to display that model's data.
Link the two application pages together so they show coordinated data.
1. Select the menu option Tools -> PointBase Network Server -> Start Server from the Sun ONE Studio to start the PointBase Network Server (database server).
Caution - Depending upon how you installed the Sun ONE Studio and the Sun ONE Application Server, you might get an error message that prohibits you from starting the PointBase Network Server. If this happens, you just need to configure it first. If you were able to start the server, skip to the JDBC Datasources section. |
2. Select Tools -> PointBase Network Server -> Configure.
A dialog displays that prompts for a file storage location and a port number.
3. Accept the defaults by clicking OK.
You receive two dialogs warning that a file already exists, and asking if it is OK to overwrite them.
4. Click OK to overwrite both of them.
Your PointBase Network Server is ready to be started.
You can create a JDBC Datasource using the Sun ONE Application Framework JDBC Datasource wizard.
However, by default, one was created for you that points to the PointBase sample database that ships with the Sun ONE Studio.
If you need to create additional JDBC Datasources for a different database other than the one used in the tutorial, use the following steps.
(Otherwise, read over to become familiar with this topic, or skip to the Tomcat (and other non-JNDI containers) SQL Connection Preparation section.)
1. Under the Sun ONE Application Framework Web application node (Jato Tutorial), expand the Settings & Configuration folder.
2. Expand the Design-Time Resources folder.
3. Right-click the JDBC Datasources node.
The Define JDBC datasource panel displays.
5. Enter the preferred datasource name in the New datasource name textbox.
6. In the Select connection combo box, select the appropriate JDBC connection.
If the connection you need does not exist, you must create one. This is performed by a tool that is outside the scope of the Sun ONE Application Framework tools. You need to select the Runtime tab in the Explorer window, expand the Databases node, right-click it, and select Add Connection. You might need to add a driver for your database before you can add a connection. See the Sun ONE Studio online help for more details.
A new JDBC Datasource node is created.
1. Expand the Application Classes folder.
2. Expand the jatotutorial package folder.
3. Double-click the JatoTutorialAppServletBase class to open it.
There is a lot of commented-out event code in here with comments describing what you can do with the events. Ignore them as you do not need any of them for this tutorial application.
You need to add a static initializer to perform the following:
a. Instruct the Sun ONE Application Framework not to use JNDI lookups
b. Load the PointBase JDBC driver
c. Map the JDBC Datasource (jdbc/jdbc-pointbase) to the PointBase sample database's JDBC connection URL
The following code sample shows the code that needs to be added to the JatoTutorialAppServletBase class. Only the bold code needs to be added. Much of the code/comments from the JatoTutorialAppServletBase class has been omitted here.
Your application will now use a JDBC URL directly to make a connection to the database instead of using the connection pooling via JNDI.
Important: If you value performance in your Web application, use JNDI for production.
You can get the PointBase client library from the following directory:
<studio-install-dir>/appserver7/pointbase/server/lib
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.