java jdbc

14
JDBC (Java Database Connectivity)

Upload: ankit-desai

Post on 16-Apr-2017

777 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: java Jdbc

JDBC(Java Database Connectivity)

Page 2: java Jdbc

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

Page 3: java Jdbc

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

Page 4: java Jdbc

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

Page 5: java Jdbc

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

Page 6: java Jdbc

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

Page 7: java Jdbc

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

Page 8: java Jdbc

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

Page 9: java Jdbc

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

Page 10: java Jdbc

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

Page 11: java Jdbc

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

Page 12: java Jdbc

Classes / Interfaces (java.sql package)

07/01/16 12

Page 13: java Jdbc

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

Page 14: java Jdbc

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