Login    New User    Forgot Password    Help      

Java Database Connectivity

Java Tutorials

What is JDBC?

JDBC is Java application programming interface that allows the Java programmers to access database management system from Java code. It was developed byJavaSoft, a subsidiary of Sun Microsystems.

Definition
Java Database Connectivity
 in short called as JDBC. It is a java API which enables the java programs to execute SQL statements. It is an application programming interface that defines how a java programmer can access the   database in tabular format from Java code using a set of  standard interfaces and classes written in the Java programming language.

JDBC has been developed under the Java Community Process that allows multiple implementations to exist and be used by the 
same application. JDBC provides methods for querying and  updating the data in Relational Database Management system  such as SQL, Oracle etc. 

The Java application programming interface provides a mechanism for dynamically loading the correct Java packages and drivers and registering them with  the JDBC Driver Manager that is used as a connection factory for creating JDBC connections which supports creating and executing statements such as SQL INSERT, UPDATE and DELETE. Driver Manager is the backbone of the jdbc architecture.

Generally all Relational Database Management System supports SQL and we all know that Java is platform independent, so JDBC  makes it possible to write a single database application that can run on different platforms and interact with different Database Management Systems. 

Java Database Connectivity is similar to Open Database Connectivity (ODBC) which is used for accessing and managing database, but the difference is that JDBC is designed specifically for Java programs, whereas ODBC is not depended upon any language. 

In short JDBC helps the programmers to write java applications that manage these three programming activities:

1. It helps us to connect to a data source, like a database.
2. It helps us in sending queries and updating statements to the database and 
3. Retrieving and processing  the results received from the database in terms of answering to your query.

DBC is an API specification developed bySun Microsystems that defines a uniform interface for accessing various relational databases. JDBC is a core part of the Java platform and is included in the standard JDK distribution.

The primary function of the JDBC API is to provide a means for the developer to issue SQL statements and process the results in a consistent, database-independent manner. JDBC provides rich, object-oriented access to databases by defining classes and interfaces that represent objects such as:

  1. Database connections
  2. SQL statements
  3. Result Set
  4. Database metadata
  5. Prepared statements
  6. Binary Large Objects (BLOBs)
  7. Character Large Objects (CLOBs)
  8. Callable statements
  9. Database drivers
  10. Driver manager

The JDBC API uses a Driver Manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The Driver Manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases. The location of the driver manager with respect to the JDBC drivers and the servlet is shown in the picture below

JDBC driver translates standard JDBC calls into a network or database protocol or into a database library API call that facilitates communication with the database. This translation layer provides JDBC applications with database independence. If the back-end database changes, only the JDBC driver need be replaced with few code modifications required. There are four distinct types of JDBC drivers.

First Step towards JDBC

Introduction

This article introduce you with JDBC and shows you how to create a database application to access the databases. For the shake of simplicity, in very first example Access database and Sun's JDBC-ODBC drivers are used. In the later sections we will show you how to use JDBC from your servlets or JSP pages to create dynamic pages or to store the data received from visitors.

What is JDBC?

Java Database Connectivity or JDBC for short is set of Java API's that enables the developers to create platform and database independent applications in java. The biggest advantage of programming in Java is its platform independence. An application written to access the MS Access database on Win 95/Win NT platform can work on Linux against Oracle database, only by changing the name of driver, provided none of the database calls it makes are vendor specific.

What are JDBC Drivers?

JDBC Drivers are set of classes that enables the Java application to communicate with databases. Java.sql that ships with JDK contains various classes for using relational databases. But these classes do not provide any implementation, only the behaviours are defined. The actual implementaions are done in third-party drivers. Third party vendors implements the java.sql.Driver interface in their database driver. 

JDBC Drivers Types

Sun has defined four JDBC driver types. These are:

  1. Type 1: JDBC-ODBC Bridge Driver
    The first type of JDBC dirver is JDBC-ODBC Bridge which provide JDBC access to any ODBC complaint databases through ODBC drivers. Sun's JDBC-ODBC bridge is example of type 1 driver.
  2. Type 2: Native -API Partly - Java Driver
    Type 2 drivers are developed using native code libraries, which were originally designed for accessing the database through C/C++. Here a thin code of Java wrap around the native code and converts JDBC commands to DBMS-specific native calls.
  3. Type 3: JDBC-Net Pure Java Driver
    Type 3 drivers are a three-tier solutions. This type of driver communicates to a middleware component which in turn connects to database and provide database connectivity.
  4. Type 4: Native-Protocol Pure Java Driver
    Type 4 drivers are entirely written in Java that communicate directly with vendor's database through socket connection. Here no translation or middleware layer, are required which improves performance tremendously.

Database Example with MS Access

// Input output
import java.lang.*;
import java.sql.*;
class employee
{

public static void main(String args[]) throws Exception
{
String Eno, Ename , Ejob;

Connection Con = null;

Statement Stmt;
ResultSet Rs;

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Con=DriverManager.getConnection("jdbc:odbc:employe");
Stmt=Con.createStatement();
Rs=Stmt.executeQuery("select * from employee");

// for convert it into oracle
//Con=DriverManager.getConnection("jdbc:odbc:MYDSN", "system","manager");
//Stmt=Con.createStatement ();
//Rs=Stmt.executeQuery("SELECT EMPNO,ENAME,JOB FROM EMPLOYEE");

System.out.println("=========================");
System.out.println("records of Employee Table");
System.out.println("=========================");
System.out.println("Employee ID "+ " "+"name"+" "+"Job");
System.out.println("----------------------------------------------");
while(Rs.next())
{
Eno=Rs.getString(1);
Ename=Rs.getString(2);
Ejob= Rs.getString(3);
System.out.println(Eno + " " + Ejob+"\n");
}
Stmt.close();
Con.close();
}

catch(Exception e)
{
System.out.println(e);
}

}
}

Click here Download MS Acess File

Output

Java Tutorials