JDBC stands for Java Database Connectivity. It is an API (Application Programming Interface) in Java that allows Java applications to interact with databases. JDBC provides a set of interfaces and classes for connecting to a database, executing SQL queries, and retrieving results.
It is part of the Java Standard Edition platform and is designed to be database-independent, enabling Java developers to write database code that can work with different database management systems (DBMS) without significant changes.
Key features of JDBC include:
- Connection Management: Establishing and managing a connection to a database.
- Statement Execution: Executing SQL queries and updates using various types of statements.
- Result Set Handling: Retrieving and processing the results of queries.
- Transaction Management: Managing transactions to ensure data integrity.
- Metadata Access: Retrieving metadata about the database and its structure.
Here is the code snippet to get the basic information about JDBC driver such as the driver name and the version.
package org.kodejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
public class JDBCDriverInfo {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "kodejava";
private static final String PASSWORD = "s3cr*t";
public static void main(String[] args) {
try (Connection conn =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DatabaseMetaData metadata = conn.getMetaData();
String driverName = metadata.getDriverName();
String driverVersion = metadata.getDriverVersion();
int majorVersion = metadata.getDriverMajorVersion();
int minorVersion = metadata.getDriverMinorVersion();
System.out.println("Driver Name = " + driverName);
System.out.println("Driver Version = " + driverVersion);
System.out.println("Major Version = " + majorVersion);
System.out.println("Minor Version = " + minorVersion);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
The result of our program:
Driver Name = MySQL Connector/J
Driver Version = mysql-connector-j-8.4.0 (Revision: 1c3f5c149e0bfe31c7fbeb24e2d260cd890972c4)
Major Version = 8
Minor Version = 4
Maven Dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>