0% found this document useful (0 votes)
81 views25 pages

JDBC Session

This document provides an overview of JDBC (Java Database Connectivity) including its components, architecture, and how to create a basic JDBC application. It discusses the DriverManager, drivers, connections, statements, result sets, transactions, and savepoints. The key steps to create a JDBC application are importing necessary packages, registering a JDBC driver, opening a connection, executing queries, extracting result sets, and closing resources.

Uploaded by

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

JDBC Session

This document provides an overview of JDBC (Java Database Connectivity) including its components, architecture, and how to create a basic JDBC application. It discusses the DriverManager, drivers, connections, statements, result sets, transactions, and savepoints. The key steps to create a JDBC application are importing necessary packages, registering a JDBC driver, opening a connection, executing queries, extracting result sets, and closing resources.

Uploaded by

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

JDBC (Connection

Classes)

Yatin Niimesh
GS, Associate Consultant

October 2013

What is JDBC

JDBC is a Java-based data access technology (Java Standard Edition platform).


This technology is an API for the Java programming language that defines how a client may
access a database.
It provides methods for querying and updating data in a database. JDBC is oriented
towards relational databases.
Java can be used to write different types of executables, such as Java Applications, Java
Applets, Java Servlets, Java Server Pages (JSPs), Enterprise JavaBeans (EJBs).
All of these different executables are able to use a JDBC driver to access a database and take
advantage of the stored data.

JDBC Architecture

Components of JDBC

DriverManager
Driver
Connection
Statement
Resultsets
SQL Exception

Create JDBC Application

Import the packages . Requires that you include the packages containing the JDBC classes
needed for database programming. Most often, using import java.sql.* will suffice.
Register the JDBC driver . Requires that you initialize a driver so you can open a
communications channel with the database.
Open a connection . Requires using the DriverManager.getConnection() method to create a
Connection object, which represents a physical connection with the database.

Execute a query . Requires using an object of type Statement for building and submitting
an SQL statement to the database.
Extract data from result set . Requires that you use the
appropriate ResultSet.getXXX() method to retrieve the data from the result set.

Clean up the environment . Requires explicitly closing all database resources versus
relying on the JVM's garbage collection.

JDBC Drivers
Type 1, Type 2, Type 3, Type 4

JDBC Database Connections

Import JDBC Packages:


To use the standard JDBC package, which allows you to select, insert, update, and delete
data in SQL tables, add the following imports to your source code:

import java.sql.* ;
Register JDBC Driver: Two Approaches
Approach (I) - Class.forName():

JDBC Database Connections


Approach (II) - DriverManager.registerDriver():

Database URL Formulation:


Establish a connection using the DriverManager.getConnection()method.
getConnection(String url)
getConnection(String url, String user, String password)
Here each form requires a database URL.

A database URL is an address that points to your database.

Creating Connection object

10

JDBC Statements
The JDBC Statement, CallableStatement and PreparedStatement interfaces define
the methods and properties that enable you to send SQL or PL/SQL commands and
receive data from your database.

Creating Statement Object:

11

JDBC Statements

Statement object is used to execute a SQL statement with one of its three execute
methods.
boolean execute(String SQL)
int executeUpdate(String SQL)

ResultSet executeQuery(String SQL)

Creating PreparedStatement Object:

12

JDBC Statements
The CallableStatement Objects:
It creates the CallableStatement object which would be used to execute a
call to a database stored procedure.

Creating CallableStatement Object:

13

JDBC Statements

Connection.prepareCall() method to instantiate a CallableStatement object


based on the preceding stored procedure:

14

JDBC - Result Sets

The SQL statements that read data from a database query return the data
in a result set.

The SELECT statement is the standard way to select rows from a database
and view them in a result set.
Thejava.sql.ResultSet interface represents the result set of a database
query.
A ResultSet object maintains a cursor that points to the current row in the
result set. The term "result set" refers to the row and column data
contained in a ResultSet object.

15

JDBC - Result Sets

ResultSet interface
Navigational methods: used to move the cursor around.
Get methods: used to view the data in the columns of the current row
being pointed to by the cursor.
Update methods: used to update the data in the columns of the current
row. The updates can then be updated in the underlying database as well.
JDBC provides following connection methods to create statements with
desired ResultSet:
createStatement(int RSType, int RSConcurrency);
prepareStatement(String SQL, int RSType, int RSConcurrency);
prepareCall(String sql, int RSType, int RSConcurrency);
16

JDBC - Result Sets


Type of ResultSet:

17

JDBC - Result Sets


Concurrency of ResultSet:

18

JDBC - Result Sets


Navigating a Result Set:

19

JDBC - Result Sets


Viewing a Result Set:

20

JDBC - Result Sets


Updating a Result Set:

21

JDBC - Transactions

Transactions enable you to control if, and when, changes are applied to the
database.

It treats a single SQL statement or a group of SQL statements as one


logical unit, and if any statement fails, the whole transaction fails.
setAutoCommit() method
conn.setAutoCommit(false);

22

JDBC - Transactions
Commit & Rollback

23

JDBC - Transactions
Using Savepoints:
setSavepoint(String savepointName): defines a new savepoint. It also returns a Savepoint object.
releaseSavepoint(Savepoint savepointName): deletes a savepoint. Notice that it requires a Savepoint object as a
parameter. This object is usually a savepoint generated by the setSavepoint() method.
There is one rollback ( String savepointName ) method which rolls back work to the specified savepoint.

24

25

You might also like