_Java DataBase Connectivity (JDBC)
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks commonly associated with database usage:
- Making a connection to a database
- Creating SQL statements
- Executing that SQL queries in the database
- Viewing & Modifying the resulting records
JDBC Architecture:
Java application calls the JDBC library. JDBC loads a driver which talks to the database. We can change database engines without changing database code.
JDBC mainly contains two things.
- JDBC API
- JDBC Driver
JDBC Drivers:
A database API, or driver, defines methods and properties that enable you to send, retrieve, and obtain status information about the database as well as extract data. You can obtain database drivers from a variety of sources.
JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server. For example, using JDBC drivers enable you to open data base connections and to interact with it by sending SQL or database commands then receiving results with Java.
JDBC driver types:
JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Thus, Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, whose characteristics vary greatly.
Type-1 Driver: JDBC-ODBC Bridge:-
This category works with ODBC drivers supplied by your database vendor or a third party. Although you may find ODBC implementations for UNIX, they are usually used with Microsoft Windows. To use the bridge, you must first have an ODBC driver specifically for your database and any additional software that you need for connectivity. Figure shows how a client interacts with the database using the JDBC−ODBC bridge.
Type 2: JDBC−native API
This category requires an operating system–specific API that handles the database communications. Usually the vendor implements the API using C/C++ so it will be optimized for the client’s environment. As a developer, you do not have to worry about the details of the native API, a Type 2 driver handles the interaction. Figure illustrates a Type 2 JDBC driver configuration.
Type 3: 100% Pure Java, JDBC−network
Type 3 drivers use a three−tier approach to accessing databases. J2EE deployments often implement this architecture. Clients use standard network sockets to communicate with an application server. The socket information is then translated by the application server into the call format required by the DBMS, and forwarded to the database server. Figure 4−5 shows a typical three−tier configuration that uses a Type 3 driver for database connectivity.
Type 4: 100% Java
Type 3 drivers use a three−tier approach to accessing databases. J2EE deployments often implement this architecture. Clients use standard network sockets to communicate with an application server. The socket information is then translated by the application server into the call format required by the DBMS, and forwarded to the database server. Figure 4−5 shows a typical three−tier configuration that uses a Type 3 driver for database connectivity.
Each type of driver has its benefits and weaknesses. Your job as a developer is to understand them so you can make good design decisions.