Java data access

Page 42

Chapter 3: Setting Up Your First JDBC Query In This Chapter • Understanding JDBC configuration issues • Obtaining and installing JDBC drivers • Identifying common JDBC components • Creating a simple JDBC application • Understanding the steps used in a JDBC application Database programming may seem daunting at first glance. After all, it encompasses many facets, such as client/server communications, drivers, APIs, incompatible data types, and SQL statements. You may think you need to know about all these issues before you can start to develop database applications. Frankly, you do need to know something about them in order to create reliable database applications; however, JDBC minimizes the learning curve for those just getting started. The JDBC API abstracts much of the work needed to create robust database applications. Its core components consist of simple, intuitively named objects that do the work for you. To create an application, you just configure and assemble the components in the correct order. However, if you move into JDBC enterprise development, things change a little. You use different objects for opening database connections, but their functionality remains the same. JDBC programming is very methodical. Ninety percent of JDBC applications use the same objects and methods regardless of what you want to accomplish. For example, you always load a driver, open a connection, submit an SQL statement, and examine the results. The details of each step vary very little from task to task. In this chapter, I guide you through building a simple JDBC application from start to finish. I start by discussing how to configure JDBC. Next, I identify the common components used in all JDBC applications, and then I present a sample application and cover the discrete steps involved in it. Lastly, I cover how to compile and run a JDBC application as well as how to troubleshoot any problems that may occur.

Configuring JDBC JDBC is an API that encapsulates the low−level calls needed for database access and interaction into one common interface. Both the Java Development Kit (JDK) and Java Runtime Environment (JRE) contain the API as part of the standard distribution. The API’s interfaces and classes reside in the java.sql and javax.sql packages. The standard components are packaged in java.sql while the enterprise elements are in javax.sql. The JDBC API differs from a JDBC driver. The API defines the interfaces and methods vendors implement when writing a driver. If you examine the API source code you will find it consists mainly of interfaces. As a result, before you can write a JDBC application, you need to obtain and install a JDBC driver, which implements the interfaces. However, a single JDBC driver does not enable you to access different “brands” of databases. In other words, you cannot access an SQL Server database using an Oracle driver. You must use a driver specifically targeted for your database.

30


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.