top of page
Writer's pictureShruti Vaghasiya

What is JDBC? and java database connectivity steps.

  • Java Database Connectivity (JDBC) is an API that allows Java programs to interface with relational databases.

  • It includes a set of classes and interfaces for connecting to databases, running SQL queries, and retrieving and manipulating database data.

  • JDBC is intended to be database independent, allowing developers to create database applications that can operate on any platform and connect to any database that has a JDBC driver.


Key Components of JDBC:


1. JDBC Driver:

  • JDBC-ODBC Bridge Driver:

    • The JDBC-ODBC Bridge Driver translates JDBC calls to ODBC calls. Mainly utilized for local development; not suitable for production.

  • Native-API Driver:

    • Converts JDBC calls to database-specific native calls.

  • Network Protocol Driver:

    • Converts JDBC calls to a database-independent network protocol, which is subsequently transformed back to database-specific calls by the server.

  • Thin Driver:

    • A pure Java driver that transforms JDBC calls directly to the database-specific protocol. Widely utilized in manufacturing settings.


2. JDBC API:

  • DriverManager: Controls a list of database drivers. It connects to the database by selecting the proper driver.

  • Connection: This represents a connection to a certain database.

  • Statement: The statement is used to execute SQL queries.

    • Statement: For running simple SQL queries.

    • PreparedStatement: Used to execute pre-compiled SQL queries with arguments.

    • CallableStatement: Used to execute stored procedures.

  • ResultSet: This is the result set of a query.

  • SQLException: Handle SQL failures and warnings.


These five steps will guide you through connecting any Java program to a database using JDBC. The steps are as follows:


1. Register for the Driver class.

2. Create a connection.

3. Create a statement.

4. Execute queries.

5. Close the connection.


1. Register the driver class

To register the driver class, use the Class.forName() function. This method dynamically loads the driver class.


2. Create connection

To establish a connection with the database, use the getConnection() function of the DriverManager class.


3. Create a statement

To make statements, utilize the Connection interface's createStatement() function. The statement's object is responsible for executing queries with the database.


4. Execute queries

To perform queries to the database, utilize the executeQuery() function of the Statement interface. This function returns the ResultSet object, which may be used to retrieve all of a table's records.


5. Close the connection

By closing the connection object statement, the ResultSet will be immediately closed. To close a connection, use the Connection interface's close() method.


Example:



9 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Opmerkingen


bottom of page