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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.7 Bey<strong>on</strong>d the Basics319files, and even when it is in files <strong>on</strong> the filesystem, you cannot, when you installa package like BudgetPro, simply put your database files in place, since thereare probably other applicati<strong>on</strong>s that have their tables in those files, and replacingthem with yours would destroy that data.Often, the table creati<strong>on</strong> statements and any initial table rows required areplaced in a SQL file and that file is run against the database. Meanwhile, all ofthe database code that performs applicati<strong>on</strong> interacti<strong>on</strong>s is either in the applicati<strong>on</strong>code or in stored procedures called by the applicati<strong>on</strong> code. But there isno fundamental reas<strong>on</strong> to make this distincti<strong>on</strong>. The applicati<strong>on</strong> can see to itthat its database and tables are created.Of course, you can automate this setup with a shell script, but Java issupposed to be cross-platform. Of course, you can write a batch file for Windowsand a shell script for UNIX, but if you just put this setup into your Javacode, you d<strong>on</strong>’t have to maintain and test separate installati<strong>on</strong> procedures. Oneof the areas where Java applicati<strong>on</strong>s tend to lag behind other applicati<strong>on</strong>s is ininstallati<strong>on</strong> and setup. You can obviate this somewhat by including databasesetup in your Java code, thus eliminating the need to write platform-specificscripts.C<strong>on</strong>sider including your database and table creati<strong>on</strong> statements directlyin your Java code, even if it is in a singlet<strong>on</strong> setup class that <strong>on</strong>ly runs <strong>on</strong>ce.The basic tables should parallel the objects. So, for our classes, the SQLstatements to build the tables might look as shown in Example 14.1.For the moment, we are c<strong>on</strong>fining our table declarati<strong>on</strong>s to a form thatshould work with both Open Source databases.These are very basic definiti<strong>on</strong>s. We will be talking about issues like generatingunique IDs for records as we develop the code to back these. Differentdatabase products have different “best” soluti<strong>on</strong>s, which will make the supportfor multiple databases more problematic.14.7BEYOND THE BASICSWe are going to adopt a very simple strategy for database persistence. We aregoing to read in the data structures at startup and maintain them as changesare made during executi<strong>on</strong>. That way, any abnormal terminati<strong>on</strong> will leave thedata in a recoverable state. The applicati<strong>on</strong> will not require any “save”operati<strong>on</strong>.

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

Saved successfully!

Ooh no, something went wrong!