Chapter 4

Using JDBC to Connect to a Database

IN THIS CHAPTER

check Configuring JDBC drivers

check Creating a connection

check Executing SQL statements

check Retrieving result data

check Updating and deleting data

JDBC — Java Database Connectivity — is a Java feature that lets you connect to almost any relational database system, execute SQL commands, and process the results, all from within a Java program. In this chapter, you set up JDBC and use it to access data in a MySQL database.

If you aren’t familiar with the basics of SQL, read the previous chapter before tackling this chapter.

Setting Up a Driver

Before you can write a Java program to access a database via JDBC, you must first install a driver that links Java’s database API classes to an actual database. Correctly setting up the driver can be tricky, but once you get it working, accessing the database is easy.

To install the JDBC driver for MySQL, browse to www.mysql.com/products/connector and click the link for JDBC Driver for MySQL (Connector/J)

Get Java All-in-One For Dummies, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.