Chapter 4
Using JDBC to Connect to a Database
In This Chapter
Configuring JDBC drivers
Creating a connection
Executing SQL statements
Retrieving result data
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)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access