Tutorials: Automate Data Persistence with Firestorm/DAO:

An Extended Example

The following example shows you how to create a sample database in MySQL and generate DAO classes for the selected tables.

The sample database is a bare bones version of a "company" database. It's made up of four tables—employee, product, sales, and customer. The reason it only has four tables is that the evaluation version of FireStorm does not allow you to generate code for more than three tables in a database.

Prerequisites

  • MySQL ODBC Connector (MyODBC)
  • MySQL Connector/J (Type 4 JDBC driver)
  • Configure FireStorm DAO (Options—>Configure FireStorm/DAO): Specifying the location of J2EE.jar is absolutely essential. If you want to use Log4J as the logging framework, specify the location to log4j.jar.
  • Configure the JDBC Drivers for MySQL in FireStorm (Options—>Configure JDBC Drivers) If this is already configured, validate it and make sure that the driver is being loaded successfully.

Generating the Code

  1. Create a database in MySQL named 'company' with the following tables or import the SQL script provided to create the required tables. Listing 1 shows the tables' structure.
  2. Next create a data source named 'company' that points to the company database in MySQL.
  3. Start Firestorm and create a new project. Click JDBC on the toolbar. This starts the Database Import wizard. Choose MySQL datasource as the JDBC datasource and click Next. All available MySQL databases are displayed. Choose company and move to the next screen. Deselect Import Views and Import Stored Procedures, as they are not in the database. Let the Map to Java Primitive types where possible option remain selected.
  4. Move through the remaining screens and click Finish to complete the import of our database.
  5. Navigate through the tree view control and verify if the SQL Type in the Columns is mapped to its correct Java and JDBC types. Go back if there is a problem in the mapping.
  6. Define finder methods for the chosen tables. Depending on the business requirement, you can define any number of finder methods. Usually, finder methods are based on the Primary key.
  7. Click the Generate button on the toolbar to start generating code. Choose DAO tier for J2EE.
  8. Enter the settings for the project including the logging framework, the application server on which it will be deployed, the data access framework, and the presentation framework. In this case the settings were:
    • Application Name: sample
    • Package: com.test.sample
    • Logging: System.out.println()
    • Application Server: Jakarta Tomcat 5
    • Persistence Framework based on JDBC
    • Web Application based on Struts 1.1
  9. FireStorm generates 5,639 lines of Java code.
  10. Browse to the folder where FireStorm generated the code. DAO code for three tables can be found here. Firestorm also generates an ANT script that can be used to compile these files and creates a WAR file out of them. This WAR file is deployable on Apache Tomcat. Users can make small modifications to the ANT script and deploy it on any application server.
  11. Listing 2 shows an edited version of the generated DAO interface class.

References

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.