Java Reference
In-Depth Information
14.9.4
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.
Straight JDBC
14.9.4.1
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.
Static Account Members
14.9.4.2
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.
Joining the User
14.10
R
EVIEW
We briefly examined choices of Open Source SQL database servers. We
described how to set up PostgreSQL.