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

ch5Database

The document provides an overview of Java Database Connectivity (JDBC), detailing its architecture, types of JDBC drivers, and the components of the JDBC API. It explains the differences between JDBC and ODBC, and outlines the steps for establishing a connection to a database using JDBC. Key interfaces such as DriverManager, Connection, Statement, and ResultSet are also discussed, along with the process for executing SQL statements.

Uploaded by

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

ch5Database

The document provides an overview of Java Database Connectivity (JDBC), detailing its architecture, types of JDBC drivers, and the components of the JDBC API. It explains the differences between JDBC and ODBC, and outlines the steps for establishing a connection to a database using JDBC. Key interfaces such as DriverManager, Connection, Statement, and ResultSet are also discussed, along with the process for executing SQL statements.

Uploaded by

alemunuruhak9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Java Database Connectivity

1
Database and SQL

 SQL commands are divided into four categories


1. DDL (Data Definition Language)

 Define data (create table)

2. DML (Data Manipulation Language)

 Insert ,Delete and update

3. DCL (Data Control Language)

 Grant

4. Query (select) 2
 JDBC is an API used to connect Java application with database

 Is used to interact with various type of database i.e. Ms Access, MYSQL and SQL
Server
Java Application
 JDBC Architecture

JDBC API

JDBC DriverManager

JDBC Driver JDBC Driver


3
What is ODBC

 Before JDBC, ODBC API was used to communicate with the databases

 ODBC API uses ODBC drivers to interact with the databases.

 Written in c language

 Platform dependent

 Unsecured

 ODBC requires manual installation of ODBC driver

4
JDBC Driver

 Is a software component that enables java application to interact with the database .

 There are four types of JDBC drivers


1. JDBC-ODBC bridge driver
‾ Uses ODBC driver to connect to the database
‾ It converts JDBC method calls into the ODBC function calls
‾ Easy to use
‾ Can be easily connected to any database
‾ Performance degraded
‾ The ODBC driver needs to be installed
5
2. Native – API driver
‾ Uses client side libraries of the database
‾ Converts JDBC method calls into native calls of the database
‾ It is written in java
‾ Performance upgraded than JDBC-ODBC bridge driver
‾ The native driver needs to be installed

3. Network protocol driver


‾ Uses middleware (Application server) that converts JDBC calls into database protocol
‾ Written in java
‾ No client side library is required
‾ Network support is required
6
‾ Maintenance of network protocol driver becomes costly
4. Thin driver
‾ Thin driver converts JDBC calls into database protocol
‾ Written in java
‾ Better performance that all other drivers
‾ No software is required
‾ Drivers depends on the database

7
JDBC API

 Is a java application programming interface to develop DBMS independent java


applications using a uniform interface.

 Consists of classes and interfaces

The driver Interface

 The driver interface is database specific

 The driver must be loaded before connecting to a database, to locate, load and link the
driver class.

8
The DriverManager class

 A layer of JDBC working b/n the driver and the user

 It keep track the registered drivers connection between the database and the driver

The Connection Interface

 A connection instance represents a session with a specific database

 SQL statements are sent to the database for execution and returns the results of the
execution

9
The Statement Interface

 Is used to execute a static SQL statement

 executeQuery

 ExecuteUpdate

The ResultSet Interface

 Provides access to a table of data generated by executing a statement

 It maintains a cursor pointing to its current row of data

 Cursor movement methods (absolute, first, last, next and previous)

10
The SQL Exception classes

 Provides information on a database access error.

The DatabaseMetaData Interface

 Enables to obtain information about the database

 E.g. tables, columns, PK, FK

11
1. Register the driver
 Class.forName() is used to load the driver

 E.g. Class. forName(“Com.mysql.jdbc.Driver”);

2. Create a connection
 getConnection() method of DriverManger class is used to create a connection

 getConnection(String url, String user, String Pass);

 E.g. getConection(“Jdbc:mysql, localhost:3306/Student, “user”, “pass””);

3. Create SQL statement


 createStatment() method is invoked on current connection object to create a SQL statement.
12
 Statement s= con.createStatement();
4. Execute SQL statement
 executeQuery() method of statement interface is used to execute SQL statement

 E.g. ResultSet rs= s.executeQuery(“select * from user”);

5. Closing the connection


 The close() method connection interface is used to close the connection.

13

You might also like