Database programming using JDBC-An Introduction

Database programming using JDBC-An Introduction

Even though Java has many features, it became famous and widely accepted in the industry because of its platform independency.  The acceptance does not become hundred percent complete unless business community endorses java.  Every enterprise (business) application is required to access the database either for retrieving the data to be processed or for storing the processed data.  Java offers a clean and simple approach for database connectivity through JDBC using which a java application can connect virtually to any database.  Therefore enterprises endorsed java and rest is the history.

What is JDBC?

JDBC is a trademark name from Sun. It is not an acronym for Java Database Connectivity. JDBC is an API. Using this API we can develop java applications that can communicate with databases. JDBC API support is made available to Java applications through 2 packages.

  1. java.sql
  2. javax.sql

java.sql package offers a set of fundamental interfaces and classes used for java-database communication. Extension package is used for advanced Java-database operations.


Java Application: – It can be a stand-alone java program, a servlet, a jsp, a Java bean or an EJB, which uses the JDBC API to get connected and perform operations on the database data.

JDBC API: It is a set of classes and interfaces used in a java program for database operations. java.sql & javax.sql packages provide the necessary library support.

DriverManager: – Java program uses DriverManager to get the connection with the database.

Driver: – It is the software that establishes connection with the database.  It is the translation software that translates the JDBC method calls. This software enables the communication between java program and the database.

Database: – Enterprise data is stored in the database.

Standard steps to connect to the database from a Java program

  1. Loading the driver
  2. Preparing the connection string
  3. Requesting for the connection
  4. After performing database operations closing the connection.

Loading the Driver:

Important entity in the Java-Database communication is the driver. JDBC driver is a class. We have to load this Java class programmatically into memory.  We have so many kinds of drivers. Initially we make use of Sun Microsystems developed driver. Fully qualified name of that driver class is “sun.jdbc.odbc.JdbcOdbcDriver”. To load the class dynamically, we use the following static method.   Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Preparing the connection string:

We need to supply the database URL for connectivity purpose. This URL is known as connection string. When we are using Sun driver, connection string will be in the following format.

String cs=”jdbc:odbc:dsn”;

Data Source Name (DSN) we have to configure externally. After configuring, we give some name to the configuration. That name we use in the connection string.

Requesting for connection:

Java program calls the following method on the DriverManager to get the database connection.

Connection con=DriverManager.getConnection(cs,”username”,”password”);

DriverManager initializes the connection process on the driver. Driver creates the connection and gives to the DriverManager. In turn, it gives to the Java program.

Closing the connection:

After performing database operations (Insertion, deletion, updation or selection) we close the database connection.

Task: – stand-alone Java application that connects to the oracle database.

  Source code:
import java.sql.*;
class ConnectToOracle
  public static void main(String args[]) throws Exception
          String driver=”sun.jdbc.odbc.JdbcOdbcDriver”;
          String cs=”jdbc:odbc:student”;
          String user=”scott”;
          String pwd=”tiger”;
          System.out.println(“Driver loaded”);
          Connection con=DriverManager.getConnection(cs,user,pwd);
          System.out.println(“Connected to the oracle database”);

If the above program is successfully executed, we get the following output.

  Driver loaded

  Connected to the oracle database

Observations to be made

  1. In the connection string, “student” is the DSN. We need to configure it using Data Source Administrator.
  2. The user name “scott” and password “tiger” is the default user name and password to login to oracle database. The database administrators change them. Accordingly we have to change them in our program.
  3. forName method throws ClassNotFoundException. getConnection and close methods throw SQLException. Either we have to handle them explicitly by writing try catch OR pass on them by using throws class for the main. Otherwise, compiler generates syntactical errors and forces us to deal with exceptions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: