java jdbc

Post on 16-Apr-2017

777 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JDBC(Java Database Connectivity)

Introduction• It is an API (Application Programming Interface)• Used to connect JAVA application with database• Interact with different types of databases like, MS Access, MySQL, Oracle, SQL Server, PostgreSQL, Sybase

07/01/16 2

JDBC Driver• Type-1 Driver ( JDBC-ODBC Bridge)• Type-2 Driver (Native-API Partly Java Driver / Partly Java)• Type-3 Driver (Network Protocol Driver / Pure Java to Middleware)• Type-4 Driver (Thin Driver / Pure Java Direct to Database)

07/01/16 3

Type-1 Driver ( JDBC-ODBC Bridge)• Act as a bridge between JDBC and other database connectivity mechanism (ODBC) • Converts JDBC calls into ODBC calls and redirects the request to ODBC driver• Sun provides a JDBC-ODBC Bridge Driver

07/01/16 4

Type-1 Driver ( JDBC-ODBC Bridge) cont…• Advantages

Easy to useAllow easy connectivity to all database supported by the ODBC

Driver• Disadvantages

Slow execution timeDependent on ODBC DriverUses Java JNI (Native Interface) to make ODBC call

07/01/16 5

Type-2 Driver (Native-API Partly Java Driver / Partly Java)• Converts JDBC calls into calls to the

client API for that database• Client JDBC Driver Vendor Client DB Library DB

07/01/16 6

Type-2 Driver (Native-API Partly Java Driver / Partly Java) cont…• Advantages

Better performance than Type-1 because no JDBC to ODBC translation is needed.• Disadvantages

Vendor client library needs to be installed on the client machine.Can not be used in web-based application due the client side s/w needed.Not all database have a client side library.

07/01/16 7

Type-3 Driver (Network Protocol Driver / Pure Java to Middleware)• Follow three tier communication approach• Can interface to multiple databases• Client JDBC Driver Middleware-Net Server Any Database

07/01/16 8

Type-3 Driver (Network Protocol Driver / Pure Java to Middleware) cont…• Advantages

Does not require any native library to be installed.Database IndependencyProvide facility to switch over from one database to another

database• Disadvantages

Slow due to increase number of network call

07/01/16 9

Type-4 Driver (Thin Driver / Pure Java Direct to Database)• Interact directly with database• Does not require any native

database library• Called pure Java Driver• Also known as Thin Driver

07/01/16 10

Type-4 Driver (Thin Driver / Pure Java Direct to Database)• Advantages

Does not require any native libraryDoes not require any Middleware serverBetter Performance than other driver

• DisadvantagesSlow due to increase number of n/w callAt client side, a separate driver is needed for each database

07/01/16 11

Classes / Interfaces (java.sql package)

07/01/16 12

Connection InterfaceMethod Description

void close() This method frees the connection object’s database and other JDBC resources

void commit() This method makes all the changes made since the last commit or rollback. It throws SQLException

boolean isClosed() This method returns “true” if the connection is close else returns “false”

void rollback() This method undoes all changes made to the databaseStatement createStatement() This method creates a Statement object for sending SQL statements

to the database. throws SQLException

CallableStatement prepareCall(String s)

This method creates a CallableStatement object for calling stored procedures. It throws SQLException

PreparedStatementprepareStatement(String s)

This method creates a PreparedStatement object for sending SQL statements with or without IN parameter. It throws SQLException.

07/01/16 13

Statement InterfaceMethod Description

void close() this method releases the statement object’s database and JDBC resources

boolean execute(String s) This method executes the SQL statement specified by s.

ResultSet executeQuery(String s) This method executes the SQL statement specified by s and returns the ResultSet object

int executeUpdate(String s) This method executes the SQL statement specified by s. These statements may be INSERT, UPDATE or DELETE

int getMaxRows() This method returns the maximum number of rows that are generated by the executeQuery() method

ResultSet getResultSet() This method retrieves the ResultSet generated by the execute() method

07/01/16 14

top related