JDBC API Components [ 2024 ]
JDBC API Components
In this tutorial, you will learn about JDBC API components. JDBC stands for Java Database Connectivity. JDBC API is a collection of classes and interfaces that allow Java applications to interact with databases. Using the JDBC API, we can access any database.
The core JDBC API is comprised of two main packages. We automatically get both packages when we download Java on the machine. The two packages are as follows:
- java.sql;
- javax.sql;
JDBC Components
The main JDBC API components are as follows:
- JDBC Driver
- DriverManager
- Connection
- Statement
- ResultSet
- ResultSet Metadata
JDBC Driver
A JDBC driver is a software component that implements the JDBC API for a specific database system. For example, MySQL JDBC driver for Java programming language is MySQL Connector/J
MySQL Connector/J is the official JDBC driver for the MySQL database. It allows Java applications to connect to and interact with the MySQL database.
Different types of JDBC drivers are available.
- Type 1 (JDBC-ODBC Bridge)
- Type 2 (Native API)
- Type 3 (Network Protocol)
- Type 4 (Thin Driver or Pure Java Driver)
DriverManager
DriverManger manages the JDBC drivers and establishes the connection to the database. It uses the JDBC URL to establish the connection and returns the Connection object on a successful database connection.
Connection
The Connection object represents the session to the connected database. The createStatement() method creates a Statement object to send SQL statements to the database.
Statement
The Statement object executes the SQL statements and returns the SQL query results.
Example: stmt.executeQuery(sqlQuery)
ResultSet
The ResultSet interface in JDBC represents the result set of a database query. It refers to the SQL query result and maintains a cursor pointing to the current row. We can iterate through the ResultSet using a while loop to get the data.
- A ResultSet object maintains a cursor pointing to its current row of data. Initially, the cursor is positioned before the first row. You can use the next() method to move the cursor to the next row.
- You can retrieve data from the current row using getter methods like getString(), getInt(), etc. These methods can take either the column index or the column name as an argument.
Java Tutorials
Java Tutorial on this website:
For more information on Java, visit the official website :
More information about Oracle Database: