JDBC

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

JDBC

What is JDBC
JDBC Stands for Java Database Connectivity The JDBC API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files.

JDBC Drivers
To use the JDBC API with a particular database management system, you need a JDBC technology-based driver to mediate between JDBC technology and the database. Depending on various factors, a driver might be written purely in the Java programming language or in a mixture of the Java programming language and Java Native Interface (JNI) native methods.

Type of JDBC Drivers


Type 1 - drivers that implement the JDBC API as a mapping to another data access API, such as ODBC. Drivers of this type are generally dependent on a native library, which limits their portability. The JDBC-ODBC Bridge driver is an example of a Type 1 driver. Type 2 - drivers that are written partly in the Java programming language and partly in native code. These drivers use a native client library specific to the data source to which they connect. Again, because of the native code, their portability is limited.

Type of JDBC Drivers Contd


Type 3 - drivers that use a pure Java client and communicate with a middleware server using a database-independent protocol. The middleware server then communicates the clients requests to the data source. Type 4 - drivers that are pure Java and implement the network protocol for a specific data source. The client connects directly to the data source.

How to work with JDBC API


By using JDBC API with the appropriate driver for your DBMS you can perform operation on your database a basic pattern
Establish a connection with a data source Send queries and update statements to the data source Process the results

JDBC Portability
To make JDBC DBMS independent it defines a set of interfaces that developers will irrespective of which driver they are using Under the wraps the database specific driver provides the implementation for these interfaces

Commonly Used Interfaces in JDBC


java.sql.Connection
A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connection.

java.sql.Statement
The object used for executing a static SQL statement and returning the results it produces.

java.sql.ResultSet
A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

The DriverManager Class


The basic service for managing a set of JDBC drivers. Database connections are obtained from this class. Its getConnection methods attempts to return the correct connection based on the loaded Drivers and the parameters passed it.

Basic Steps with JDBC

When using JDBC prior to obtaining a Connection one needs to load the driver class. Once that is done the connection can be obtained and further steps can be carried out as shown.

Types of Statement
Statement PreparedStatement
These are precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

CallableStatement
Used to execute SQL stored procedures.

More on ResultSet object


A ResultSet object maintains a cursor pointing to its current row of data. Calling the next() method moves the cursor to the next row if there is a next row and returns true in case the cursor was incremented. A default ResultSet object is not updatable and has a cursor that moves forward only.

Types of ResultSet objects


TYPE_FORWARD_ONLY
A ResultSet object whose cursor may move only forward

TYPE_SCROLL_INSENSITIVE
A ResultSet object that is scrollable but generally not sensitive to changes made by others.

TYPE_SCROLL_SENSITIVE
A ResultSet object that is scrollable and generally sensitive to changes made by others.

Batch Update Insertion


Use addBatch() method to add a insert/update statement When done adding statements to Batch, call executeUpdate()

Discussion and Practical

You might also like