Before you start
This second part in a two-part tutorial series that demonstrates the creation of a wiki system using Derby. Part 1 discussed the setup, configuration, and how to embed Derby as a database within a Web application server. It also covered some basics of regular expressions for the purposes of parsing wiki text.
This tutorial examines functionality within the Derby database, including examples of how to use functions and stored procedures to provide additional functionality within your database and how to use triggers to provide preprocessing or post-processing to your SQL Insert/Update/Delete statements.
This tutorial is designed to provide you with a basic understanding of and overall perspective on how to implement functions, stored procedures, and triggers within your applications when using Derby as a database. Many developers don't know when or why to use these database features, so this tutorial provides a few ideas about how to take advantage of a function or a trigger. The tutorial is structured as follows:
- Overview -- Defines database functions, stored procedures, and triggers.
- Java functions -- Explains what Java™ functions in the database can be used for and provides an example from the wiki application.
- Java procedures -- Covers Java stored procedures in the database, how to define input and output parameters in Derby, and an example of a procedure from the wiki application.
- Database triggers -- Identifies different types of triggers supported by Derby and shows an example of what types of triggers can be used within the wiki application.
This tutorial assumes you have a basic knowledge of the Java Database Connectivity (JDBC) API. It's also beneficial to have some familiarity with the concepts of database triggers, functions, and procedures. But if you don't, this tutorial defines and describes each of these in enough detail to ensure that you feel comfortable with these topics.
To run the example code in this tutorial, perform the following steps:
- Download and install the following applications
- Make sure the environmental variables outlined in Table 1 are defined in your shell.
Table 1. Set the environmental variables Variable name Required setting TOMCAT_HOME Set to the root folder of your Tomcat installation DERBY_HOME Set to the root folder of your Derby installation ANT_HOME Set to the root folder of your Ant installation JAVA_HOME Set to the root folder of your Java installation PATH Ensure that ANT_HOME/bin is in your PATH
- Extract the supplied .zip file (see the Download section for the file) to your preferred location (this will be the project root).
The project is laid out as follows:
/Wiki/ /conf/ > contains DDL (data definition language) /deploy/ > created by ANT (will contain WAR file) /src/ > contains Java source code /web/ > contains JSPs and web.xml, etc. /build.xml > ANT build file
- The last thing you need to do is modify the /web/WEB-INF/web.xml file to specify where you want Derby to create your wiki database. In my case, I've set the
derby.system.homeproperty in the web.xml file to beW:\Development\WikiDB. See the following example taken from the web.xml file:<servlet> <servlet-class>net.humandoing.wiki.WikiServlet</servlet-class> <servlet-name>WikiServlet</servlet-name> <init-param> <param-name>derby.system.home</param-name> <param-value>W:\Development\WikiDB</param-value> </init-param> </servlet>
Note: Make sure that the value you specify is a directory and that it already exists. Second, you don't need to put a trailing slash. Third, if you are on a *nix-based system, your path would look something like /development/WikiDB, but you can create this directory wherever you want to.
If you don't specify a value or a valid value for this property, the application creates the wiki database under the Tomcat directory webapps/wiki/WEB-INF/WikiDB. This is OK, but it means that if you redeploy the wiki, any data that you entered into your wiki will be gone.
Let's continue where we left off from Part 1 and start the wiki (see the Download section for the application) by pointing your browser to http://localhost:8080/wiki. You should receive the default wiki homepage as shown in Figure 1.
Figure 1. Default index page of the sample wiki application
Now that you've seen what the wiki index looks like when it's first started up with no data, let's look at the functions, procedures, and triggers you're going to add.

