JDBC Setup and Connection

Problem

You want to access a database via JDBC.

Solution

Use Class.forName( ) and DriverManager.getConnection( ).

Discussion

While DB and friends have their place, most of the modern database action is on relational databases, and accordingly Java Database action is on JDBC. So the bulk of this chapter is devoted to JDBC.

This is not the place for a tutorial on relational databases. I’ll assume that you know a little bit about the Structured Query Language (SQL), the universal language used to control relational databases. SQL has queries like “SELECT * from userdb”, which means to select all columns (the *) from all rows (entries) in a database table named userdb (all rows are selected because there is no “where” clause on the SELECT statement). SQL also has updates like INSERT, DELETE, CREATE, and DROP. If you need more information on SQL or relational databases, there are many good books that will introduce you to the topic in more detail.

JDBC has two Levels, JDBC 1 and JDBC 2. Level 1 is included in all JDBC implementation and drivers; Level 2 is optional, and requires a Level 2 driver. This chapter concentrates on common features, primarily Level 1.

Get Java Cookbook 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.