Java Database Connectivity (JDBC) provides a database programming API for Java programs. A JDBC API contains a set of classes and interfaces that are used to connect to a database built using any DBMS/RDBMS. It also submits SQL queries to a database, and retrieves and processes the results of SQL queries. JDBC are platform independent and vendor independent.
JDBC drivers are divided into four categories. Each category defines JDBC driver implementation with increasingly higher levels of platform independence,
performance, and deployment administration. The JDBC API is based mainly on a set of interfaces, not classes. It's up to the manufacturer of the driver to implement the interfaces with their own set of classes
The four categories of JDBC drivers are:
§ Type 1: JDBC-ODBC bridge
§ Type 2: Native-API/partly-Java driver
§ Type 3: Net-protocol/all-Java driver
§ Type 4: Native-protocol/all-Java driver
Basic steps in writing a JDBC application
§ Load the appropriate Driver and then register for a connection object. The Class.forName(..) will load the Driver and register it with the DriverManager.The driver is the core component for JDBC. Drivers are written by vendors and must support the basic features of the JDBC specification.
§ Create Connection to the Database using the DriverManager. There are two ways to establish a connection with the database. DataSource interface provides a alternative to the DriverManager for making the connection. Datasource makes the code more portable than the DriverManager because it works with the JNDI and it is created, deployed and managed separately from the application that uses it. Connection is a Java interface providing pipeline between our code and database.
§ Create statement using the connection object. A Statement is a Java interface that represents messages sent from your code to the database.
§ Execute a query and generate a ResultSet instance. A ResultSet is a Java interface representing a set of data drawn from the database.
simple application that connects to an ODBC database and performs a select statement on a table called Employee
__________________________________________________________
import java.sql.*;
public class HelloJDBCApplication {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection conn = DriverManager.getConnection(
"jdbc:odbc:dandelion", "sa", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT empID,
empFname, empLname ,salary FROM Employee");
System.out.println("EmpID \t EmpName \t
Salary");
while (rs.next()) {
System.out.println(rs.getInt("empID") +
"\t" + rs.getString("empFname") + rs.getString("empLname")
"\t" + rs.getLong("salary"));
}
}catch (SQLException se) {
System.out.println("SqlException: " + se.getMessage());
se.printStackTrace(System.out);
}catch (ClassNotFoundException e) {
System.out.println("ClassNotFound: " + e.getMessage());
} finally{
rs.close();
stmt.close();
conn.close();
}
} //main
} //class
__________________________________________________________
JDBC Architecture
JDBC architecture decouples an abstraction from its implementation so that the implementation can very independent of the abstraction. This is the example of the bridge design pattern. The JDBC API provides the abstraction and JDBC Driver provide the implementation. New Drivers can be plugged into the JDBC API without changing the client code.