Java Reference
In-Depth Information
Most popular database management systems provide JDBC drivers. In this chapter,
we introduce JDBC and use it to manipulate Java DB databases. The techniques we dem-
onstrate here can be used to manipulate other databases that have JDBC drivers. If not,
third-party vendors provide JDBC drivers for many DBMSs. This chapter's examples were
tested with Java DB using both the Java SE 7 and Java SE 8 JDKs.
Java Persistence API (JPA)
In online Chapter 29, we introduce Java Persistence API (JPA). In that chapter, you'll
learn how to autogenerate Java classes that represent the tables in a database and the rela-
tionships between them—known as object-relational mapping—then use objects of those
classes to interact with a database. As you'll see, storing data in and retrieving data from a
database will be handled for you—the techniques you learn in this chapter will typically
be hidden from you by JPA.
24.2 Relational Databases
A relational database is a logical representation of data that allows the data to be accessed
without consideration of its physical structure. A relational database stores data in tables .
Figure 24.1 illustrates a sample table that might be used in a personnel system. The table
name is Employee , and its primary purpose is to store the attributes of employees. Tables
are composed of rows , each describing a single entity—in Fig. 24.1, an employee. Rows
are composed of columns in which values are stored. This table consists of six rows. The
Number column of each row is the table's primary key —a column (or group of columns)
with a value that is unique for each row. This guarantees that each row can be identified
by its primary key. Good examples of primary-key columns are a social security number,
an employee ID number and a part number in an inventory system, as values in each of
these columns are guaranteed to be unique. The rows in Fig. 24.1 are displayed in order
by primary key. In this case, the rows are listed in ascending order by primary key, but they
could be listed in descending order or in no particular order at all.
Number
Name
Department
Salary
Location
23603
24568
34589
35761
47132
78321
Jones
Kerwin
Larson
Myers
Neumann
Stephens
413
413
642
611
413
611
1100
2000
1800
1400
9000
8500
New Jersey
New Jersey
Los Angeles
Orlando
New Jersey
Orlando
Row
Primary key
Column
Fig. 24.1 | Employee table sample data.
Each column represents a different data attribute. Rows are unique (by primary key)
within a table, but particular column values may be duplicated between rows. For
example, three different rows in the Employee table's Department column contain number
413.
 
 
Search WWH ::




Custom Search