Java Database Connectivity B Ramamurthy 992020 B Ramamurthy

  • Slides: 24
Download presentation
Java Database Connectivity B. Ramamurthy 9/9/2020 B. Ramamurthy 1

Java Database Connectivity B. Ramamurthy 9/9/2020 B. Ramamurthy 1

Introduction Most popular form of database system is the relational database system. Examples: MS

Introduction Most popular form of database system is the relational database system. Examples: MS Access, Sybase, Oracle, MS Sequel Server. Structured Query Language (SQL) is used among relational databases to construct queries. These queries can be stand-alone or embedded within applications. This form of SQL is known as embedded SQL. 9/9/2020 B. Ramamurthy 2

Simple Database Application A P P L I C A T I O N

Simple Database Application A P P L I C A T I O N 9/9/2020 DBMS DB Ex: Access Oracle Sybase B. Ramamurthy 3

Multi-Databases A P P L I C A T I O N 9/9/2020 B.

Multi-Databases A P P L I C A T I O N 9/9/2020 B. Ramamurthy DBMS 1 DB DBMS 2 DB DBMS 3 DB 4

Standard Access to DB A P P L I C A T I O

Standard Access to DB A P P L I C A T I O N 9/9/2020 D R I V E R DBMS Driver 1 DBMS 1 DB DBMS Driver 2 DBMS 2 DB M G R DBMS Driver 3 DBMS 3 DB B. Ramamurthy 5

ODBC Architecture Application Class 1 Class 2 ODBC Driver Manager Driver. Type 1 Data.

ODBC Architecture Application Class 1 Class 2 ODBC Driver Manager Driver. Type 1 Data. Source 1 9/9/2020 Driver. Type 2 Driver. Type 3 Data. Source 2 B. Ramamurthy Data. Source 3 6

Open Database Connectivity (ODBC) Standard ODBC standard is an interface by which application programs

Open Database Connectivity (ODBC) Standard ODBC standard is an interface by which application programs can access and process SQL databases in a DBMS-independent manner. It contains: A Data Source that is the database, its associated DBMS, operating system and network platform A DBMS Driver that is supplied by the DBMS vendor or independent software companies A Driver Manager that is supplied by the vendor of the O/S platform where the application is running 9/9/2020 B. Ramamurthy 7

ODBC Interface It is a system independent interface to database environment that requires an

ODBC Interface It is a system independent interface to database environment that requires an ODBC driver to be provided for each database system from which you want to manipulate data. The database driver bridges the differences between your underlying system calls and the ODBC interface functionality. 9/9/2020 B. Ramamurthy 8

An Example rd a d Application c b d n a t s o

An Example rd a d Application c b d n a t s o Sybase driver 9/9/2020 I P A Driver. Manager m. SQL driver B. Ramamurthy Informix driver 9

Application in Java I P A I d r P a A d n

Application in Java I P A I d r P a A d n c a b t d s j c Application in db Driver. Manager o Java Sybase driver 9/9/2020 m. SQL driver B. Ramamurthy Informix driver 10

Java Support for ODBC : JDBC When applications written in Java want to access

Java Support for ODBC : JDBC When applications written in Java want to access data sources, they use classes and associated methods provided by Java DBC (JDBC) API. JDBC is specified an an “interface”. An interface in Java can have many “implementations”. So it provides a convenient way to realize many “drivers” 9/9/2020 B. Ramamurthy 11

Java Support for SQL Java supports embedded SQL. Also it provides an JDBC API

Java Support for SQL Java supports embedded SQL. Also it provides an JDBC API as a standard way to connect to common relational databases. You need a JDBC: ODBC bridge for using the embedded SQL in Java. sql package and an extensive exception hierarchy. We will examine incorporating this bridge using sample code. 9/9/2020 B. Ramamurthy 12

Data Source Local relational database; Ex: Oracle Remote relational database on a server; Ex:

Data Source Local relational database; Ex: Oracle Remote relational database on a server; Ex: SQLserver On-line information service; Ex: Dow Jones, Customer database 9/9/2020 B. Ramamurthy 13

Data Source and Driver Data source is the data base created using any of

Data Source and Driver Data source is the data base created using any of the common database applications available. Your system should have the driver for the database you will be using. For example your Windows system should have the MS Access Driver. There a number of JDBC drivers available. Information on installing them is available at : http: //industry. java. sun. com/products/jdbc/drivers 9/9/2020 B. Ramamurthy 14

JDBC Components Driver Manager: Loads database drivers, and manages the connection between application &

JDBC Components Driver Manager: Loads database drivers, and manages the connection between application & driver. Driver: Translates API calls to operations for a specific data source. Connection: A session between an application and a driver. Statement: A SQL statement to perform a query or an update operation. Metadata: Information about the returned data, driver and the database. Result Set : Logical set of columns and rows returned by executing a statement. 9/9/2020 B. Ramamurthy 15

JDBC Classes Java supports DB facilities by providing classes and interfaces for its components

JDBC Classes Java supports DB facilities by providing classes and interfaces for its components Driver. Manager class Connection interface (abstract class) Statement interface (to be instantiated with values from the actual SQL statement) Result. Set interface 9/9/2020 B. Ramamurthy 16

Driver Manager Class Provides static, “factory” methods for creating objects implementing the connection interface.

Driver Manager Class Provides static, “factory” methods for creating objects implementing the connection interface. n Factory methods create objects on demand when a connection is needed to a DB driver, Driver. Manager does it using it factory methods. 9/9/2020 B. Ramamurthy 17

Connection interface Connection class represents a session with a specific data source. Connection object

Connection interface Connection class represents a session with a specific data source. Connection object establishes connection to a data source, allocates statement objects, which define and execute SQL statements. Connection can also get info (metadata) about the data source. 9/9/2020 B. Ramamurthy 18

Statement interface is implemented by the connection object. Statement object provides the workspace for

Statement interface is implemented by the connection object. Statement object provides the workspace for SQL query, executing it, and retrieving returned data. SELECT {what} FROM {table name} WHERE {criteria} ORDER BY {field} Queries are embedded as strings in a Statement object. Types: Statement, Prepared. Statement, Callable. Statement 9/9/2020 B. Ramamurthy 19

Result. Set interface Results are returned in the form of an object implementing the

Result. Set interface Results are returned in the form of an object implementing the Result. Set interface. You may extract individual columns, rows or cell from the Result. Set using the metadata. 9/9/2020 B. Ramamurthy 20

JDBC Application Architecture Application Connection Statement Result Set Driver Manager Driver Data. Source 9/9/2020

JDBC Application Architecture Application Connection Statement Result Set Driver Manager Driver Data. Source 9/9/2020 Driver Data. Source B. Ramamurthy Data. Source 21

JDBC Programming Steps Import necessary packages; Ex: import java. sql. *; Load JDBC driver(driver

JDBC Programming Steps Import necessary packages; Ex: import java. sql. *; Load JDBC driver(driver should have been installed) Data source and its location should have been registered. Allocate Connection object, Statement object and Result. Set object Execute query using Statement object Retrieve data from Result. Set object Close Connection object. 9/9/2020 B. Ramamurthy 22

Identifying Data Sources It is specified using URL format. <scheme>: <sub_scheme>: <scheme-specific -part> Example(for

Identifying Data Sources It is specified using URL format. <scheme>: <sub_scheme>: <scheme-specific -part> Example(for local source): jdbc: odbc: tech_books Alternatively, for remote connection, jdbc: odbc: //bina. cse. buffalo. edu: 4333/tech_boo ks 9/9/2020 B. Ramamurthy 23

Summary JDBC API of Java language conforms to the ODBC standard. It supports ODBC

Summary JDBC API of Java language conforms to the ODBC standard. It supports ODBC by providing classes/interfaces for driver manager, driver, connection, statement and resultset and others. You can access various components in JDBC by instantiating or implementing objects and accessing their methods. Go through the tutorial at: http: //developer. java. sun. com/developer/online. Training/Database/JDBC 20 Intro/ 9/9/2020 B. Ramamurthy 24