< Zurück | Inhalt | Weiter >

14.9.4 Straight JDBC

Our application is quite simple. We’re going to directly integrate database access by simply calling the JDBC interface at startup and making use of that connection again and again for data changes.

Our solution is more than sufficient for the standalone command-line and GUI versions of the application, but it will be left as an exercise for the reader to implement a better solution for the EJB implementation.

What will be lacking? Well, the solution we will implement here will get a single database connection and use it throughout the life of the application. It will be assumed that a single thread is accessing the database. These are all bad assumptions for a multiuser and multithreaded environment such as an application server. Static Account Members

Most of the database code is in the Account class. It consists of a static method, getTopAccount(), which will establish a static connection to the database, create the Account table if need be, and load the top level account (defined as the account record with a null parent) if present or create it if not present.

The username is passed in as an argument. The username must exist in the User table. If it does not, an exception is thrown. Joining the User

Two static methods are added that take a JDBC Connection and a String as arguments. They are getUserIdByName() and getUserByName(). The String is the username to look for. The first method returns the id column for that user. It returns zero (0) if the user doesn’t exist. The other returns a User object, or null if the user doesn’t exist.

14.10 REVIEW

We briefly examined choices of Open Source SQL database servers. We described how to set up PostgreSQL.


This is another chapter that represents a mere gloss of its topic. Visit your local book megamart and just count the number of books on Oracle, MySQL, and PostgreSQL. Database servers and relational database design are very large topics indeed. We have hardly scratched the surface. Our goal here has been merely to give you enough to get started. If you plan to use one of these prod- ucts, definitely take the time to download and read the official product docu- mentation (for both MySQL and PostgreSQL it is actually quite good), and then take your time looking for after-market books that seem to meet your needs.


By no means have we read all the titles available on this topic, but we have read and can recommend:

• Leon Atkinson, Core MySQL, ISBN 0-13-066190-2.

• Paul DuBois, MySQL, ISBN 0-7357-0921-1.

• Korry Douglas and Susan Douglas, PostgreSQL, ISBN 0-7357-1257-3.


Is this database fully normalized?13 If not, what would need to be done to normalize it? What reasons might exist for not fully normalizing a database?


13. “Normalization” is a process of eliminating database redundancy and of theoretical optimization of the data model. A decent introduction to the concepts may be found at http://dev.mysql.com/tech-resources/articles/intro-to-normalization.php.