You are on page 1of 29

ODBC and JDBC

OBJECT DATABASE CONNECTIVITY (ODBC)


 ODBC: Object Database Connectivity
 A standard or open application programming interface
(API) for accessing a database
 Allows programs to use SQL requests that will access
databases without knowledge of the proprietary
interfaces to the databases
 Handles these requests and converts it into a request
understandable by an individual database system
 Ability to connect to a multitude of data sources using
a single API.
 Based on the Call Level Interface specified by the SQL
Access Group.
ODBC REQUIREMENTS

• An ODBC software for a particular OS.


• A separate module or driver for each database to be
accessed
 Using vendor-supplied drivers to translate the ODBC
commands into something understood by the DBMS, a
single application can connect and communicate with
multiple databases using a single, common set of
commands.
ODBC ARCHITECTURE
JAVA DATABASE CONNECTIVITY (JDBC)
 JDBC acronym of java Database connectivity;
though Sun Microsystems claims that it is not the full
form.
 JDBC is a standard java API for independent database
connection between a java program and wide range of
relational database.
 It is present in the “java.sql” package
 Uses the same underlying concept of ODBC, but offers
the portability of the Java programming language.
 4 different kinds exist that depend on the JVM and third
party software to different degrees.
COMPONENTS OF JDBC:
JDBC has four main components as under and with the
help of these components java application can connect with
database.

 The JDBC API - it provides various methods and interfaces for


easy communication with database.
 The JDBC DriverManager - it loads database specific
drivers in an application to establish connection with
database.
 The JDBC test suite - it will be used to test an operation
being performed by JDBC drivers.
 The JDBC-ODBC bridge - it connects database drivers to
the database
ARCHITECTURE OF JDBC
 As we all know now that driver is required to
communicate with database.
 JDBC API provides classes and interfaces to
handle request made by user and response
made by database.
Some of the important JDBC API are as under.

DriverManager Driver
Connection Statement
PreparedStatement CallableStatement
ResultSet DatabaseMetaData
ResultSetMetaData

http://www.java2all.com
 As per the architecture first of all we have to
program our application with JDBC API.

 With the help of DriverManager class than we


connect to a specific database with the help of
spcific database driver.

 Java drivers require some library to


communicate with the database.

 We have four different types of java


drivers.

http://www.java2all.com
TYPES OF JDBC DRIVER

 Type 1; jdbc-odbc bridge driver


 Type 2; native API partly java driver.
 Type 3; net protocols all java driver.
 Type 4; native protocols all java driver.
JDBC TYPE 1: JDBC-ODBC BRIDGE

 JDBC calls are translated into ODBC function calls.


 In this case, JDBC is a layer on top of ODBC.
 Sun provides a JDBC-ODBC Bridge driver by
“sun.jdbc.odbc.JdbcOdbcDriver”.
 Requires at least that ODBC binaries be installed on
each machine where an app using this type of JDBC is
installed.
 Type 1 is the simplest compare to all other driver but it’s
a platform specific i.e. only on Microsoft platform.
 The JDBC-ODBC Bridge is use only when there is no
PURE-JAVA driver available for a particular database.
JDBC TYPE 2: NATIVE - API DRIVER

 Similar to the previous type in that binaries must


be installed in addition to any apps that use this
type of JDBC.
 Native APIs are supplied by vendors for
translating JDBC calls into a form understood by
their proprietary DBMSs.
 There is no implantation of JDBC-ODBC Bridge
so it’s faster than a type 1 driver; hence the
performance is better as compare the type 1
driver (JDBC-ODBC Bridge).
JDBC TYPE 3: PURE JAVA DRIVER FOR
DATABASE MIDDLEWARE

 All client-side code is written in pure Java that


sends commands to a middleware program that
translates them into a form understood the
DBMS.
 According to Oracle, the middleware offers the
ability to connect to many different databases.
 There is no need for the vendor database library
on the client machine because the middleware
is database independent and it communicates
with client
JDBC TYPE 4: DIRECT-TO-DATABASE PURE
JAVA DRIVER

 All code for the driver is written purely in Java.


 JDBC calls are converted directly into a vendor's
proprietary network protocol for communicating with
their DBMS.
 Allows client apps to communicate directly with their
backend server.
 It’s a 100% pure JAVA Driver so it’s a platform
independence
http://www.java2all.com
STEPS TO CONNECT?

 Define the connection url :


Class.forName();

 For jdbc-odbc bridge driver:-


Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
 For oracle driver:-
Class.forName(“oracle.jdbc.driver.OracleDriver”);
 For My sql driver:-
Class.forName(“com.mysql.jdbc.Driver”);
STEPS TO CONNECT?

 Established the connection:

Connection con
=DriverManager.getConnection(“url”,”user_name”,”pass”);
STEPS TO CONNECT?

 Create the Statement object:

Statement stmt=con.createStatement();
STEPS TO CONNECT?

 Execute the query:

 For the SELECT query:-


 String sql=“SELECT * FROM EMP”;
 stmt.executeQuery(sql);

 For the INSERT/UPDATE query:-


 String sql=“INSERT INTO EMP VALUES(47,’TEDDY’)”;
 stmt.executeUpdate(sql);
STEPS TO CONNECT?

 Process the result:-

ResultSet rs=stmt.execute Query(sql);


while(rs.next()){
System.out.println(rs.getInt(id));
System.out.print(rs.getString(name));
}
STEPS TO CONNECT?

 Close the connection


release all the resources that the connection
is holding.

stmt.close();
con.close();
STEPS TO CONNECT?
 Summarizing the steps for connecting java DB and inserting values in
DB, deployed on Net Beans IDE :-

Class.forName("org.apache.derby.jdbc.ClientDriver");

Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/orbacus","
teddy","teddy");

Statement
stmt=con.createStatement();
String sql=“INSERT INTO
TEDDY(47,’jani’)”;
stmt.executeUpdate(sql);

stmt.close();
con.close();
TYPES OF STATEMENT AVAILABLE

 There are three types of Statement


available in Statement class:-
 Statement
 PreparedStatement
 CallableStatement
TYPES OF STATEMENT AVAILABLE

 Statement
This represent a simple sql/mysql
statement.

Statement stmt=con.createStatement();
TYPES OF STATEMENT AVAILABLE

 PreparedStatement
this represent precompiled sql/my sql
statement which allows improved
performance. It allows to execute the query
multiple times and we can set the values
according to our need.

PreparedStatement psmt=con.prepareStatement();
TYPES OF STATEMENT AVAILABLE

 CallableStatement
This allows the access of stored
procedures; that are stored on the database.

CallableStatement csmt=con.prepareCall();

You might also like