![]() |
VOOZH | about |
JDBC is an API that helps applications to communicate with databases. It allows Java programs to connect to a database, run queries, retrieve and manipulate data. Because of JDBC, Java applications can easily work with different relational databases like MySQL, Oracle, PostgreSQL and more.
1. Application: It can be a Java application or servlet that communicates with a data source.
2. JDBC API: It allows Java programs to execute SQL queries and get results from the database. Some key components of JDBC API include
3. DriverManager: It plays an important role in the JDBC architecture. It uses some database-specific drivers to effectively connect enterprise applications to databases.
4. JDBC drivers: These drivers handle interactions between the application and the database.
The JDBC architecture consists of two-tier and three-tier processing models to access a database. They are as described below:
A Java Application communicates directly with the database using a JDBC driver. It sends queries to the database and then the result is sent back to the application. For example, in a client/server setup, the user's system acts as a client that communicates with a remote database server.
Structure:
Client Application (Java) -> JDBC Driver -> Database
In this, user queries are sent to a middle-tier services, which interacts with the database. The database results are processed by the middle tier and then sent back to the user.
Structure:
Client Application -> Application Server -> JDBC Driver -> Database
JDBC drivers are client-side adapters (installed on the client machine, not on the server) that convert requests from Java programs to a protocol that the DBMS can understand. There are 4 types of JDBC drivers:
Class/Interfaces | Description |
|---|---|
| DriverManager | Manages JDBC drivers and establishes database connections. |
| Connection | Represents a session with a specific database. |
| Statement | Used to execute static SQL queries. |
| PreparedStatement | Precompiled SQL statement, used for dynamic queries with parameters. |
| CallableStatement | Used to execute stored procedures in the database. |
| ResultSet | Represents the result set of a query, allowing navigation through the rows. |
| SQLException | Handles SQL-related exceptions during database operations. |