|[ Team LiB ]|
Chapter 18. 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 Structured Query Language (SQL) commands. 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 coverage of database programming is beyond the scope of this chapter. Java Enterprise 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. PostgreSQL, another excellent open source server, is available from http://www.postgresql.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. (Installing the driver, on the other hand, is usually easy: add it to your classpath, or just drop it in the jre/lib/ext/ directory of your Java installation.)
Once you've decided which 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.
|[ Team LiB ]|