JDBC: Everything you need to know
JDBC or Java Database Connectivity is an application programming interface(API). Therefore, it connects applications made in Java with popular databases.
What is JDBC?
Sun Microsystems developed JDBC which therefore enables standard-based ways for accessing the data by Java language.
The data standards define a set of Java interfaces. Therefore, these interfaces help application developers extract data access functionality.
Also, read about Java.
JDBC drivers are divided into four types.
- JDBC-ODBC bridge
It provides a connecting bridge between the JDBC driver and application programming interface.
Type 1 drivers are generally used when the database client libraries need to be loaded on every client machine
- Pure Java driver
It works under a three-tier architecture. The network passes the JDBC calls to the middle tier server.
Therefore, the task for this server is to translate calls to a database-specific native interface.
- Native API
It is a partial Java driver as it converts JDBC calls into database-specific calls.
- Protocol Java driver
Although, the client application needs to communicate with the servers directly. So, native protocol Java drivers convert JDBC calls into specific database calls.
Although, JDBC’s main task is to create, maintain, and support the executing statements. It uses the following classes for such tasks:
- Statement: it is sent to the database server every single time. Therefore itis a recurring step.
- Callable statement: it executes stored procedures on a database.
- Prepared statement: it uses the predetermined path and is cached. Therefore, it allows it to be executed multiple times.
Steps for Java database connectivity
The procedure to connect a Java application with any database has five steps.
- Register the Driver class
- Create a connection
- Create a statement
- Execute queries
- Close connection
Register the Driver class: The forName() method of class registers the driver class. It dynamically loads the driver.
Create connection: The getConnection() establishes the connection with the database.
Create statement: The createStatement() method of connection creates the statement. Therefore, the main task of the statement is to execute queries with the database.
Execute queries: The executeQuery() executes queries to the database.
Close connection: Close connection object statement automatically closes the other statements.
Why use Java Database Connectivity?
It helps in performing the following tasks:
- Establishes connections to the database
- Updates statements to the database
- Executes queries
- Retrieve database results
Although, the Java Database Connectivity API provides universal data access from the Java programming language. Therefore, using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which tools and alternate interfaces can be built.