Chapter 4

Using JDBC to Connect to a Database

In This Chapter

arrow Configuring JDBC drivers

arrow Creating a connection

arrow Executing SQL statements

arrow Retrieving result data

arrow 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, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.