Illustrating the examples with a real-world database (iText 5)

The main theme of the examples in this topic is movies. I’ve made a selection of 120 movies, 80 directors, and 32 countries, and I’ve put all this information in a database. The entity relationship diagram (ERD) in figure 2.2 shows how the data is organized. There are three main tables, consisting of movies, directors, and countries. Furthermore, there are two tables connecting these tables.

For the examples in this topic, we’ll use the HSQL database engine (http:// hsqldb.org/). This is a lightweight database that doesn’t need to be installed. Just add hsqldb.jar to your classpath and you’re set. You’ll find this JAR in the lib directory. The HSQL database is in the db subdirectory of the resources folder. When you execute an example using the movie database, the contents of the filmfestival.script file will be loaded into memory, and you’ll see temporary files appear in the directory as soon as you start using the database.

Film database entity relationship diagram


Figure 2.2 Film database entity relationship diagram

I wrote a couple of convenience classes to hide the complexity of the database. The abstract class DatabaseConnection wraps the java.sql.Connection class, and it’s extended by the HsqldbConnection class.

Listing 2.1 DatabaseTest.java

Listing 2.1 DatabaseTest.java

This is a small standalone example to test the database connection. It writes the 32 countries from the film_country table to a file named countries.txt.

I’ve also created a class named PojoFactory, along with a series of plain old Java objects (POJOs), such as Movie, Director, and Country. These classes hide most of the database querying. In the examples that follow, you’ll find code that looks like this:

tmp17C30_thumb

Each instance of the Movie class corresponds with a record in the film_movietitle table.

In the following sections and topics, you’ll create numerous PDF files from a database, but you’ll hardly ever be confronted with difficult database queries or database-related Java syntax. The database aspects of the examples won’t get any more complex than in the first examples of the next section.

Next post:

Previous post: