Chapter 17. Accessing Databases with JDBC

Topics in This Chapter

  • Connecting to databases: the seven basic steps

  • Simplifying JDBC usage: some utilities

  • Using precompiled (parameterized) queries

  • Creating and executing stored procedures

  • Updating data through transactions

  • Using JDO and other object-to-relational mappings

JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access a wide variety of SQL databases with exactly the same Java syntax. It is important to note that although the JDBC API standardizes the approach for connecting to databases, the syntax for sending queries and committing transactions, and the data structure representing the result, JDBC does not attempt to standardize the SQL syntax. So, you can use any SQL extensions your database vendor supports. However, since most queries follow standard SQL syntax, using JDBC lets you change database hosts , ports, and even database vendors with minimal changes to your code.

DILBERT reprinted by permission of United Feature Syndicate, Inc.

graphics/17inf01.gif

Officially, JDBC is not an acronym and thus does not stand for anything. Unofficially, "Java DataBase Connectivity" is commonly used as the long form of the name .

Although a complete tutorial on database programming is beyond the scope of this chapter, we cover the basics of using JDBC in Section 17.1 (Using JDBC in General), presuming you are already familiar with SQL.

After covering JDBC basics, in Section 17.2 (Basic JDBC Examples) we present some JDBC examples that access a Microsoft Access database.

To simplify the JDBC code throughout the rest of the chapter, we provide some utilities for creating connections to databases in Section 17.3 (Simplifying Database Access with JDBC Utilities).

In Section 17.4 (Using Prepared Statements), we discuss prepared statements, which let you execute similar SQL statements multiple times; this can be more efficient than executing a raw query each time.

In Section 17.5 (Creating Callable Statements), we examine callable statements. Callable statements let you execute database stored procedures or functions.

In Section 17.6 (Using Database Transactions), we cover transaction management for maintaining database integrity. By executing changes to the database within a transaction, you can ensure that the database values are returned to their original state if a problem occurs.

In Section 17.7, we briefly examine object-to-relational mapping (ORM). ORM frameworks provide a complete object-oriented approach to manage information in a database. With ORM, you simply call methods on objects instead of directly using JDBC and SQL.

For advanced JDBC topics including accessing databases with custom JSP tags, using data sources with JNDI, and increasing performance by pooling database connections, see Volume 2 of this book. For more details on JDBC, see http://java.sun.com/products/jdbc/, the online API for java.sql , or the JDBC tutorial at http://java.sun.com/docs/books/tutorial/jdbc/.



Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net