Introduction To JDBC

text zoom

Introduction To JDBC



Java Database Connectivity(JDBC) defines how a java program can communicate with a database. JDBC API has two major packages java.sql and javax.sql. JDBC architecture consist of different layers and drivers which are capable of working with any database  



JDBC Architecture
Types Of Drivers As shown above there are 4 types of JDBC drivers available.



  1. JDBC-ODBC Bridge:- The driver converts JDBC calls to the ODBC calls which actually interacts with the database. The client should have the ODBC libraries for this driver.

  2. Native API- Partly Java driver:- The driver converts JDBC calls to database specific calls. In order to use the driver the client should have database libraries.

  3. JDBC-Net-All Java:- This driver passes the JDBC calls to proxy server which communicates with the database.

  4. Native protocol- All Java:- The driver does not require anything and directly calls the database. This driver is the fastest among all the four drivers.


Steps to write JDBC programs


The following steps are required to write any JDBC specific programs in java.



  1. Load or register the driver with Class.forName().

  2. Establish Connection with the database with Connection Interface.

  3. Create Statement objects for Queries.

  4. Execute the statements which may or may not return ResultSet.

  5. Manipulate the ResultSet.

  6. Close the Statements and Connection objects.


The following code will illustrate how to write the JDBC program.


package com.visualbuilder;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class JDBCExample1 {


        public static void main(String[] args) {
                try {

                        /** Loading the ODBC- Bridge driver*/
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        /** Getting Connection*/
                                                 Connection con = DriverManager.getConnection("jdbc:odbc:MyDsn");
                        /** Creating Statement*/
                                                Statement stmt = con.createStatement();
                        /** Creating simple table*/
                                                 stmt.execute("select * from employee");
                        /** Closing the Connection*/
                                                con.close();
                                } catch (Exception e) {
                                         e.printStackTrace();
                        }


                }


       }

                    

Copyright © 2008 VisualBuilder. All rights reserved