An Overview of JDBC

JDBC is the database connectivity package included in the core Java API.[13] JDBC gives you a database-independent interface for opening a connection to a relational database, issuing SQL calls to the database, and receiving a set of data as the result. In more technical terms, JDBC acts as a Java implementation of the standard SQL call-level interface (CLI) defined by X/Open and supported by most major relational database vendors. In order to perform transactions with a specific type of database, you need to have a JDBC driver that acts as a bridge between the JDBC method calls and the native database interface.

Data Retrieval Example

Perhaps the easiest way to get started with JDBC is to see a simple example of the API in action. Example 7.1 shows a Java code segment that opens a database connection, executes a query, and iterates through the results.

Example 7-1. A Simple JDBC Database Query
// Construct the database address String dbaseURL = "jdbc:mysubprotocol://dbasehost/dbasename"; // Make the database connection Connection dbConnection = DriverManager.getConnection(dbaseURL, "dbaseuser", "dbasepasswd"); // Create a statement and execute the SQL query Statement query = dbConnection.getStatement(); ResultSet results = query.executeQuery("SELECT first_name, last_name from user_table"); // Iterate through the results and print them to standard output while (results.next()) { String fname = results.getString("first_name"); String lname = results.getString("last_name"); ...

Get Java Distributed Computing 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.