0% found this document useful (0 votes)
12 views20 pages

Lecture No 11

software evolution

Uploaded by

hira bukhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

Lecture No 11

software evolution

Uploaded by

hira bukhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

JDBC

JAVA DATABASE
CONNECTIVITY
LECTURE OBJECTIVE

JDBC Fundamentals

Steps to create Application

JDBC Driver Types

JDBC Connections

JDBC Statements

Complete Implementation of Application


INTRODUCTION TO
JDBC
WHAT IS JDBC

“It is a standard Java API for


database-independent
connectivity between the java
programming language and a
wide range of databases”
WHAT IS JDBC
JDBC or Java Database Connectivity is a Java API to connect and execute the query
with the database. It is a specification from Sun microsystems that provides a
standard abstraction(API or Protocol) for java applications to communicate with
various databases. It provides the language with java database connectivity standards.
It is used to write programs required to access databases. JDBC, along with the
database driver, can access databases and spreadsheets. The data stored in a
relational database(RDB) can be accessed with the help of JDBC APIs.
WHAT IS JDBC

1 • Connection

2 • Create SQL Database

3 • Execute SQL\Quries

4 • Viewing\Ouput
JDBC ARCHITECTURE
JDBC ARCHITECTURE

JDBC Architecture consists of two layers −


• JDBC API: This provides the application-to-JDBC Manager connection.
• JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.
COMMON JDBC COMPONENTS

• DriverManager is used to manage a list of database drivers.


• Driver is an interface that handles the communications with the database server.
• Connection is an interface that consists all the methods required to connect to a database.
STEPS TO CREATE JDBC APPLICATION
JDBC DRIVER TYPE

• JDBC drivers implement the defined interfaces in the JDBC API, for interacting with your
database server. Essentially, a JDBC driver makes it possible to do three things:
1. Establish a connection with a data source.
2. Send queries and update statements to the data source.
3. Process the results.

• There are 4 types of drivers.


JDBC CONNECTIONS

• Import JDBC Packages


• Register JDBC Driver
• Database URL Formulation
• Create a connection object
• Close
IMPORT JDBC PACKAGES

• Add import statements to Java program to import required classes in your Java code.
REGISTER JDBC DRIVER
• This step causes the JVM to load the desired driver implementation into memory so that it can
fulfill your JDBC requests.
• The most common approach to register a driver is to use Java’s forName()method to
dynamically load the driver’s class file into memory, which automatically registers it. This
method is preferable because it allows to make the driver registration configurable and
portable.
• Refer the below code.
1try {
2Class.forName(“com.mysql.jdbc.Driver");
3}
4catch(ClassNotFoundException ex) {
5System.out.println("Error: unable to load driver class!");
6System.exit(1);
7}
OPENING A CONNECTION

• This is to create a properly formatted address that points to the database to which need to
connect.
• DriverManager.getConnection() requires the complete address of the database
– Database URL
– UserName
– Password
• A table lists down the popular JDBC driver names and database URL.
EXECUTING A QUERY
CLOSE

• At the end of your JDBC program, we have to close all the database connections to end each
database session. However, if you forget, Java’s garbage collector will close the connection
when it cleans up stale objects.

1 conn.close();

You might also like