You are on page 1of 19

JDBC - ODBC

Connectivity
What is JDBC ?
 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.

3. Retrieving and processing the results received from


the database in terms of answering to your query.
Types of JDBC Drivers
 There are four types of JDBC drivers known as:
1) JDBC-ODBC bridge driver, also called Type 1.

1) Native-API /partly Java driver, also called Type 2.

1) JDBC-Net /pure Java driver, also called Type 3.

1) Native-protocol /pure Java driver, also called Type 4.


JDBC-ODBC bridge driver
 The JDBC-ODBC bridge driver uses ODBC driver to
connect to the database.
 The JDBC-ODBC bridge driver converts JDBC method
calls into the ODBC function calls.
 This is now discouraged because of thin driver.
Advantages:
 easy to use.

 can be easily connected to any database.


Disadvantages:
 Performance degraded because JDBC method call
is converted into the ODBC function calls.
 The ODBC driver needs to be installed on the client
machine.
JDBC-ODBC bridge driver
Native-API /partly Java driver
 The Native API driver uses the client-side libraries of
the database.
 The driver converts JDBC method calls into native calls
of the database API. It is not written entirely in java.
Advantage:
 Performance upgraded than JDBC-ODBC bridge
driver.
Disadvantage:
 The Native driver needs to be installed on the
each client machine.
 The Vendor client library needs to be installed on
client machine.
Native-API /partly Java driver
JDBC-Net /pure Java driver
 The Network Protocol driver uses middleware (application
server) that converts JDBC calls directly or indirectly into
the vendor-specific database protocol. It is fully written in
java.
Advantage:
 No client side library is required because of
application server that can perform many tasks like
auditing, load balancing, logging etc.
Disadvantages:
 Network support is required on client machine.
 Requires database-specific coding to be done in the
middle tier.
 Maintenance of Network Protocol driver becomes
costly because it requires database-specific coding to
be done in the middle tier.
JDBC-Net /pure Java driver
Native-protocol /pure Java driver
 The thin driver converts JDBC calls directly into the
vendor-specific database protocol. That is why it is known
as thin driver. It is fully written in Java language.

Advantage:
 Better performance than all other drivers.
 No software is required at client side or server side.
Disadvantage:
 Drivers depends on the Database.
Native-protocol /pure Java driver
Steps to Create Connection in
JDBC
 Steps to Create Connection Between Java Program and
DB:-
1) Load the Driver.

1) Make connection between the Java Program and


Database.

1) Logically Link the Two (java prgm & db) by making a


Statement Object.

1) Fire SQL Queries.


Step 1: Load the Driver
Java Drivers
Java Program
Program Database
Database
jdbc odbc

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 Where sun.jdbc.odbc may be:-
 Type 1 (DEFAULT: FREEWARE).
 Type 2 (PAID DRIVER).
 Type 3 (PAID DRIVER).
 Type 4 (PAID DRIVER).
Step 2: Make Connection

• Make connection between the Java Program and


Database.
• Connection con =
DriverManager.getConnection(“DataSource Name");
con
Database
Java Program
s1
Step 3: Making a Statement
Object
 Logically Link the Two (java prgm & db) by making a
Statement Object.

 Statement s1 = con.createStatement();
Step 4 : Fire SQL Queries

 Fire SQL Queries.


Example:
• S1.execute Query().
• S1.execute Update().
• INSERT INTO TABLE_NAME
[ (col1, col2, col3,...colN)]
VALUES (value1, value2, value3,...valueN).
• UPDATE table_name SET column1 = value1,
column2 = value2...., columnN = valueN WHERE
[condition];
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and
password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
con.close();
}
catch(Exception e){ System.out.println(e);}
}
}
Popular Databases :

1. Hibersonic
You need a Connection Strings
2. DB2 to connect with this databases
3. Infosonic
4. Postgrace
5. Progress
6. My SQL
7. ORACLE.
Connection Strings :

You might also like