In this step you will create the database control VisitDB, which runs queries against a database table containing information about visitors and number of prior visits. A database control queries a database table to store, find, and update information.
The tasks in this step are:
All Java controls (and all Java classes) must be part of a Java package.
The database control opens in Design View. The Application tab
should now look like the picture on the right.
The top-level folder GettingStarted_Control is the containing folder for the entire application. All of the source files for your application exist in this folder.
The folder MyControlProject is the control project folder. An application can contain any number of project folders and there are many different kinds of project folders, including Web projects, Web Services projects, Schema projects, and so forth.
The Modules folder is for the storage of stand-alone applications (packaged as WAR and JAR files) that can deployed parallel with your web service, if you wish.
The Libraries folder is for the storage of resources that are to be used across multiple projects, provided the resources are packaged as JAR files. For example, if you had a control or an EJB that you wanted to re-use across all your projects, you would store it in the Libraries folder.
The Security Roles folder lets you define security roles and test users for your web application. You can test the security design of your web application by logging in as a test user.
Next you will define the methods of the database control that, when invoked, execute a SQL query. A database control method consists of two parts, namely the method signature and the SQL query that is executed by the method. The first method you are going to create is called createTable, and it executes the SQL query that will create the database table VISITORS_GS_CONTROL used by the database control.
CREATE TABLE VISITORS_GS_CONTROL ( VISITOR_NAME VARCHAR(40), VISITS INT )The dialog should now look like this
In the preceding steps you learned how to add a method to a database control. Next you are going to add three additional methods in the same manner. These methods are:
SELECT VISITS FROM VISITORS_GS_CONTROL WHERE VISITOR_NAME = {name}In the Java field of the Edit SQL and Interface dialog, change the signature of the method to:
int getVisits(String name)Click OK.
INSERT INTO VISITORS_GS_CONTROL (VISITOR_NAME, VISITS) VALUES ({name}, 1)In the Java field of the Edit SQL and Interface dialog, change the signature of the method to:
int insertVisitor(String name)Click OK.
UPDATE VISITORS_GS_CONTROL SET VISITS = {visits} WHERE VISITOR_NAME = {name}In the Java field of the Edit SQL and Interface dialog, change the signature of the method to:
int updateVisits(String name, int visits)Click OK.
As you might have noticed above, arguments in the method are passed to the SQL query, and the results returned by the query are in turn returned by the method. For example, the method getVisits takes a String name as an argument. This name is passed to the SQL query, as shown below in bold:
SELECT VISITS FROM VISITORS_GS_CONTROL WHERE VISITOR_NAME = {name}
This query returns a number, which is returned as an int value by the method.
Note. The insertVisitor and updateVisits methods return the values 0 and 1, indicating whether the insertion/updating of a record failed or was successful. In this tutorial you will not explicitly test the returned values to determine success or failure.
The VisitDB control should now look like this in Design View:
Now let's build the database control to make sure it is defined correctly.
If you encounter build errors, verify that the various SQL queries and Java methods have been created correctly.