SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction to Jdbc

Posted by Amitabh Pandey Articles | Java October 10, 2011
The JDBC(Java Database Connectivity) API(Collection of classes and interfaces) is a technique through which we connect our program to the database smoothly.
Reader Level:

The JDBC(Java Database Connectivity) API(Collection of classes and interfaces) is a technique through which we connect our program to the database smoothly. Java is one of the best languages that provides a connection through to the database smoothly, in comparison to other language.

Java Program ||--->JDBC Api ||--->Driver||--->||Database Source

            Figure 1: Description of the working of Jdbc

Steps of Jdbc for connecting the database  

  1. Loading the Jdbc driver.
  2. Connection to the Dbms.
  3. Creating and executing a statement.
  4. Processing data returned by the Dbms
  5. Close the connection with the Dbms.
connection in java

We have to import the sql package, which resides in the java .lang package for performing these steps. After this step we have to write our code for connecting to any database.

  • Loading Jdbc Driver: In this step, we load the driver class at runtime which implements the Driver interface. In Java we use a class known as Class for loading any class at runtime .forName(Driver name in String) is the static method of class, Class is used to load any driver class at runtime. The return type of this method is an object of class ,Class.
  • Syntax:Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);

    The above line is used for loading any driver class at runtime. We generally do not catch the value (Object of class,Class) returned by this method.   
     

  •  Connection to the Dbms: In the second step we have to create a connection with the database. For this we use the DriverManagerclass. There is a static method,getConnection("url","user name","password")  of DriverManager class.It takes three string type arguments,and returns object of Connection.   

     Syntax:  Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","system","tiger"); 
     

  • Creating and executing a statement: Using a Connection we create statement by the method createStatement() and get the object of Statement.

    Syntax:Statement s=con.createStatement();
     

  • Processing data returned by Dbms: According to our query we use the data returned by Dbms.

In this way, we connect a java program using the Jdbc.

Simple program for creating connection in Java

import java.sql.*;
class CreateConnection
{
    public static void main(String s[])
    {
        try
        {
            //Loading the Driver
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //Creating the Connection
            Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","System","tiger");
            if(con!=null)
            {
                System.out.println("Connection Successfull");
            }
            else
            {
                System.out.println("Connection could not be established");
            }
            //closing the connection
            conn.close();
        }
        catch(SqlException e)
        {
            System.out.println("Exception occured"+ e);
        }
    }
}

After compilation and execution of above program we get the output as:

jdbc

Thank You.....

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor