Java Reference
In-Depth Information
CHAPTER 17
Database Access with SQL
This chapter shows how you can communicate with a database server using the
JDBC API of the java.sql package. JDBC is an API that allows a Java program to
communicate with a database server using SQL (Structured Query Language) com-
mands. Note that JDBC is a SQL API, not an embedded SQL mechanism for Java.
The java.sql package provides a fairly straightforward mechanism for sending
SQL queries to a database and for receiving query results. Thus, assuming that you
already have experience working with databases and SQL, this chapter should be
relatively easy to understand. On the other hand, if you have not worked with
databases before, you'll need to learn basic SQL syntax and some general database
programming concepts before you can really take advantage of the examples in
this chapter and JDBC in general. I'll try to explain some of the basic concepts as I
go along, so that you can get a sense of what is possible with JDBC, but full cov-
erage of database programming is beyond the scope of this chapter. Java Enter-
prise in a Nutshell contains a more thorough introduction to JDBC, a SQL
reference, and an API quick-reference for the java.sql package.
In order to run the examples in this chapter, you need access to a database, and
you have to obtain and install a JDBC driver for it. If you don't already have a
database server to work with, you can use one of the excellent open-source
databases available today. The examples in this chapter have been tested with
MySQL, an open-source database available from http://www.mysql.com , and with
PostgreSQL, another open-source server available at http://www.postgr esql.org .
JDBC drivers for these database servers can be downloaded from the same sites.
Note that database servers are complex pieces of software. Downloading and
installing a database may require significant effort.
Once you've decided what database server to use, read the documentation that
comes with it. Before running the examples in this chapter, you need to know
how to administer the database server. In particular, you need to know how to
create a test database (or you need to have your database administrator create one
for you). If you are new to databases, figuring out how to do this can actually be
more difficult than learning how to program with JDBC.
 
Search WWH ::




Custom Search