24.6.1 Connecting to and Querying a Database

The example of Fig. 24.23 performs a simple query on the books database that retrieves the entire Authors table and displays the data. The program illustrates connecting to the database, querying the database and processing the result. The discussion that follows presents the key JDBC aspects of the program.

Lines 3–8 import the JDBC interfaces and classes from package java.sql used in this program. Method main (lines 12–48) connects to the books database, queries the database, displays the query result and closes the database connection. Line 14 declares a String constant for the database URL. This identifies the name of the database to connect to, as well as information about the protocol used by ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.