Java Reference
In-Depth Information
list of author names, while the
AUTHOR_WORK
table contains the list of topics along
with the corresponding
AUTHOR_ID
. The
BOOK
table contains book specifics. Follow-
ing along with the
main()
method, first the
BOOK_AUTHOR
table is queried, and its
results are fetched into a
CachedRowSet
using the
queryBookAuthor()
method.
For more details regarding the use of
CachedRowSet
objects, see Recipe 13-10.
Next, another
CachedRowSet
is populated with the results of querying the
AUTHOR_WORK
and
BOOK
tables, as the
queryAuthorBook()
method is called.
At this point, there are two populated
CacheRowSet
objects, and they can now be
combined using a
JoinRowSet
. In order to do so, each query must contain one or
more columns that relate to the other table. In this case, the
BOOK_AUTHOR.ID
column relates to the
AUTHOR_WORK.AUTHOR_ID
column, so the
RowSet
objects
must be joined on those column values.
The final method that is invoked within the
main()
is
joinRowQuery().
This
method is where all the
JoinRowSet
work takes place. First, a new
JoinRowSet
is
created by instantiating a
JoinRowSetImpl()
object:
jrs = new JoinRowSetImpl();
Note
You will receive a compile-time warning when using
JoinRowSetImpl
be-
cause it is an internal SUN proprietary API. However, the Oracle version is
OracleJoinRowSet
, which is not as versatile.
Next, the two
CachedRowSet
objects are added to the newly created
JoinRowSet
by calling its
addRowSet()
method. The
addRowSet()
method
accepts a couple of arguments. The first is the name of the
RowSet
object that you
want to add to the
JoinRowSet
, and the second is an
int
value indicating the posi-
tion within the
CachedRowSet
, which contains the key value that will be used to im-
plement the join. In the solution to this recipe, the first call to
addRowSet()
passes
the
bookAuthors CachedRowSet
, along with the number
1
because the element
in the first position of the
bookAuthors CachedRowSet
corresponds to the
BOOK_AUTHOR.ID
column. The second call to
addRowSet()
passes the
au-
thorWork CachedRowSet
, along with number
2
because the element in the
second position of the
authorWork CachedRowSet
corresponds to the
AUTHOR_WORK.AUTHOR_ID
column.