0% found this document useful (0 votes)
6 views

JDBC Architecture

1. JDBC provides a way for Java programs to interact with databases to perform operations. 2. It defines interfaces for drivers, connections, statements, and results that database vendors implement to allow communication with their database. 3. The four types of JDBC drivers are: Type 1 (JDBC-ODBC bridge), Type 2 (part Java, part native code), Type 3 (network-based), and Type 4 (100% Java). Type 4 drivers are most portable and commonly used.

Uploaded by

codekaro.ravi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

JDBC Architecture

1. JDBC provides a way for Java programs to interact with databases to perform operations. 2. It defines interfaces for drivers, connections, statements, and results that database vendors implement to allow communication with their database. 3. The four types of JDBC drivers are: Type 1 (JDBC-ODBC bridge), Type 2 (part Java, part native code), Type 3 (network-based), and Type 4 (100% Java). Type 4 drivers are most portable and commonly used.

Uploaded by

codekaro.ravi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1

JDBC

----------

1.JDBC is a step by step process to intract with database from java applications inorder to perform
database operations from java applications.

2.JDBC is a technology, it will provide very good environment to connect with database from java
application inorder to perform database operations from java applications.

3.JDBC is a API[collection of classes and interfaces] it will provide environment to intract with database
from java applications inorder to perform database operations from java applications.

4.JDBC is an abstraction provided by SUN Microsystem and implemented by all database vendors
inorder to connect with database from java applications to perform database operations from java
application.

1
2

public interface Driver

--abstract methods--

class OracleDriver implements Driver

--impl by Oracle--

class MySqlDriver implements Driver

--impl by MySql--

class DB2Driver implements Driver

--impl by DB2----

There are four types of driver in JDBC:

1.Type-1 Driver

2
3

2.Type-2 Driver

3.Type-3 Driver

4.Type-4 Driver

Tyape-1 Driver:

1.It also called as bridge Driver or JDBC-ODBC Driver.

2.It was provided by SUN Microsystem as reference implementation for driver interface inorder to
provide environment for all the database vendors to implement drver interface.

3.SUN Microsystem has provided Type-1 driver in the form of sun.jdbc.odbc.JdbcOdbcDdriver in JAVA

Software along with java predefined liberary.

4.This driver was provided by SUN Microsystem with the interdependency on the Microsoft product
“ODBC Driver” [open database connectivity].

5.ODBC is provided by Microsoft and implemented by using some native implementation and it will
provide very good environment to connect with any type of database from JDBC-ODBC Driver.

6.If we want to use Type-1 driver in JDBC applications thyen we must install ODBC Driver[ODBC native
library] in our system.

7.ODBC driver is available bydefault in Microsoft product but it is not available in Non Microsoft
product bydefault , so that, Type-1 driver is dependent on Microsoft products,Therefore, Type-1 drver s
less portable drver.

8.Type-1 driver is not suitable for web applications and distributed applications.

9.Type-1 driver is suitable for standalone applications.

10.It is not for complex JDBC Applications it is for simple jdbc applications requirements.

3
4

11.Type-1 driver is slower driver,because,it must require two times conversions inorder to intract with
database from java applications.
1.From java to ODBC by JDBC-ODBC Driver

2.From ODBC to DB representation by ODBC Driver

12.It will reduce JDBC applications performance.

2.Type-2 Driver

---------------------

1.It is also called as “part java, part native driver” or simply “Native driver”.

2.Type-2 driver was prepared by using some java mplementation and by using Database vendor
provided native Liberary.

3.If we want to use Type-2 driver in JDBC applications then we have to install the respective DB vendor
provided native library in our machine.

4.when compared with type-1 driver, Type-2 driver is more portable driver.

5.when compared with type-1 driver, type-2 driver is faster driver, type-2 driver should not required two
times conversion, it is sufficient to have one time conversion to intract with database from java
applications.

6.when compared with type-1 driver, it will improve jdbc applications performance.

7.Type-2 driver is not Economical driver, it is costfull.

8.Type-2 driver is suggestible for standalone application only, not for web applications or enterprise
applications.

Type-3 Driver:

4
5

1.Type-3 driver is also called as “Network Driver”.

2.Type-1 driver and type-2 driver are suggestible for only standalone applications but, type-3 driver is
suggestible for web application and distributed applications.

3.Type-1 driver is dependent on ODBC Driver,Type-2 driver is dependent on Database vendor provided
Native Library, but Type-3 driver is dependent on Application server which we have used for our
applications.

4.Type-3 driver is more portable driver when compared with Type-1 and Type-2 Drivers.

5.Type-3 Driver is faster driver when compared with Type-1 and type-2 driver, because type-3 driver is
not required to perform two times conversion and it is not required to load DB vendor provided native
library,it is sufficient to start application server.

6.Type-3 driver will increase performance of the JDBC applications when compared type-1 and type-2
drivers.

7.Type-3 driver is able to provide very good environment to connect with multiple databases from
multiple client at a time.

8.it is not applicable for standalone applcatons,because, it must require server environment.

Type-4 Driver

1.Type-4 driver is also called as “pure java driver” or “Thin driver”.

2.It was mplemented completely on the basis of java implementations.

3.Type-4 driver is more portable driver when compared type-1,type-2,type-3.

5
6

4.Type-4 driver is suggestible for both standalone applications and web-applications,distributed


applications.

5.It is possible to use type-4 driver with or with out application server.

6.Type-4 driver is frequently used driver in the applications.

7.Type-4 drver is light weight driver and faster driver when compared with type-1 and type-2,type-3
drivers.

8.type-4 driver is most Economical driver.

Steps to Prepare First JDBC Applications

------------------------------------------------------------------------------

1.Load and Register Driver

2.Establish Connection between java application and Database

3.Create either Statement or PreparedStatement Or CallableStatement as per the requirement.

4.Write and Execute Sql Queres.

5.Close the statement and connection.

1.Load and Regster the drver.

Public static Class forName(String Driver_class_name)throws ClassNotFoundExcepton

Class c=Class.forName(“oracle.jdbc.OracleDriver”);

Internal flow of above stmt

package java.sql;

6
7

public interface Driver

--------

package oracle.jdbc;

public class OracleDriver implements Driver

static

DriverManager.registerDriver();

paclage java.sql;

public class DriverManager

public static void registerDriver(Driver d)

-------

public class JDBCApp

public static void main(String[]e)throws Exception

7
8

Class c=Class.forName("oracle.jdbc.OracleDriver");

Java.sql package

Driver[I]

DriverManager[C]

Connection[I]

Statement[I]

PreparedStatement[I]

CallableStatement[I]

ResultSet[I]

ResultSetMetaData[I]

DatabaseMetaData[I]

2.Establish Connection between java application and database

Public static Connection getConnection(String driver_url, String db_user_Name, String db_pwd)throws


SqlException

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",


"system","java")

Driver URL:

Type-4 Driver provided by Oracle

Driver Class: oracle.jdbc.OracleDriver

Driver_URL:jdbc:oracle:thin:@localhost:1521:xe

Type-4 driver provided by MySql:

DriverClass:com.mysql.jdbc.Driver

Driver_URL: jdbc:mysql://localhost:3306/db_Name

Q)In JDBC, Connection is an interface, but,how getConnection() method will create Connection object?

8
9

Ans:- JDBC is an abstraction[collection of interfaces] provided by SUN and implemented by all database
vendors.

package java.sql;

public interface Connection//sun

---services in the form of abstract methods----

package com.oracle.jdbc;

public class OracleConnectionImpl implements Connection//Oracle

---implementation for services---

package java.sql;

public class DriverManager

public static Connection getConnection(String url, String user-name, String pwd)

--Logic to identify DB_Vendor provided JDBC impl---

---Establish Connection between java application and DB by calling connect()---

Driver.connect();

Connecton con=new OracleConnectionImpl();

return con;

9
10

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",


"system","java")

3.Create either Statement or PreparedStatement or CallableStatement object as per the requirement?

Public Statement createStatement() in java.sql.Connection

Statement st=con.createStatement();

Q)In JDBC, Statement is an interface then how it is possible to create Statement Object in
createStatement() method?

Ans-

package java.sql;

public interface Statement//sun

-------

public interface Connection//sun

-------

package com.oracle.jdbc;

public class OracleStatementImpl implements Statement

public class OracleConnectionImpl implements Connection

public Statement createStatement()

10
11

Statement st=new OracleStatementImpl();

return st;

Statement st=con.createStatement();

4.write and execute Sql Queries

1.executeQuery(----)

2.executeUpdate(---)

3.execute(---)

11
12

create table emp1(id number, name varchar(40), address varchar(60));

insert into emp1 values(333,'Namita','GGN');

update emp1 set desig='php dev' where id=333;

alter table emp1 add desig varchar(60);

alter table emp1 drop column desig;

delete emp1 where id=333;

drop table emp1;

select*from emp1;

12

You might also like