13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14.10 Review32514.9.4 Straight JDBCOur applicati<strong>on</strong> is quite simple. We’re going to directly integrate databaseaccess by simply calling the JDBC interface at startup and making use of thatc<strong>on</strong>necti<strong>on</strong> again and again for data changes.Our soluti<strong>on</strong> is more than sufficient for the standal<strong>on</strong>e command-line andGUI versi<strong>on</strong>s of the applicati<strong>on</strong>, but it will be left as an exercise for the readerto implement a better soluti<strong>on</strong> for the EJB implementati<strong>on</strong>.What will be lacking? Well, the soluti<strong>on</strong> we will implement here will geta single database c<strong>on</strong>necti<strong>on</strong> and use it throughout the life of the applicati<strong>on</strong>.It will be assumed that a single thread is accessing the database. These are allbad assumpti<strong>on</strong>s for a multiuser and multithreaded envir<strong>on</strong>ment such as anapplicati<strong>on</strong> server.14.9.4.1 Static Account MembersMost of the database code is in the Account class. It c<strong>on</strong>sists of a static method,getTopAccount(), which will establish a static c<strong>on</strong>necti<strong>on</strong> to the database,create the Account table if need be, and load the top level account (defined asthe 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 inthe User table. If it does not, an excepti<strong>on</strong> is thrown.14.9.4.2 Joining the UserTwo static methods are added that take a JDBC C<strong>on</strong>necti<strong>on</strong> and a String asarguments. They are getUserIdByName() and getUserByName(). TheString is the username to look for. The first method returns the id columnfor that user. It returns zero (0) if the user doesn’t exist. The other returns aUser object, or null if the user doesn’t exist.14.10REVIEWWe briefly examined choices of Open Source SQL database servers. Wedescribed how to set up PostgreSQL.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!