0% found this document useful (0 votes)
15 views45 pages

JDBC

Uploaded by

roshan.prajapati
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)
15 views45 pages

JDBC

Uploaded by

roshan.prajapati
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/ 45

Java Database Connectivity

Types of Drivers
Connection
• A Connection is the session between java application and
database.
• The Connection interface is a factory of Statement,
PreparedStatement, and DatabaseMetaData i.e. object of
Connection can be used to get the object of Statement and
DatabaseMetaData.
• The Connection interface provide many methods for transaction
management like commit(), rollback() etc.
Methods:
public Statement createStatement(): creates a
statement object that can be used to execute SQL queries.

2) public Statement createStatement(int


resultSetType,int resultSetConcurrency): Creates a
Statement object that will generate ResultSet objects with
the given type and concurrency.
DriverManager
• The DriverManager class acts as an interface between user
and drivers.
• It keeps track of the drivers that are available and handles
establishing a connection between a database and the
appropriate driver.
• The DriverManager class maintains a list of Driver classes
that have registered themselves by calling the method
DriverManager.registerDriver().
1) public static void is used to register the given driver
registerDriver(Driver driver): with DriverManager.
2) public static void is used to deregister the given driver
deregisterDriver(Driver driver): (drop the driver from the list) with
DriverManager.
3) public static Connection is used to establish the connection
getConnection(String url): with the specified url.

4) public static Connection is used to establish the connection


getConnection(String url,String with the specified url, username and
userName,String password): password.
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.

The methods of the ResultSet interface can be broken down


into three categories −
•Navigational methods − Used to move the cursor around.

•Get methods − Used to view the data in the columns of the


current row being pointed 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 the 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);

Type of ResultSet
The possible RSType are given below. If you do not specify any ResultSet type, you will
automatically get one that is TYPE_FORWARD_ONLY.

Type Description

ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the result set.

ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can scroll forward and backward, and the
result set is not sensitive to changes made by others
to the database that occur after the result set was
created.

ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can scroll forward and backward, and the
result set is sensitive to changes made by others to the
database that occur after the result set was created.
Concurrency of ResultSet
The possible RSConcurrency are given below. If you do not specify any
Concurrency type, you will automatically get one that is
CONCUR_READ_ONLY.
Concurrency Description

ResultSet.CONCUR_READ_ONLY Creates a read-only result set. This is the


default

ResultSet.CONCUR_UPDATABLE Creates an updateable result set.


Statement stmt = conn.createStatement();
// Set auto-commit to false
conn.setAutoCommit(false);
// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(200,'Zia', 'Ali', 30)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create one more SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(201,'Raj', 'Kumar', 35)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create one more SQL statement
String SQL = "UPDATE Employees SET age = 35 " + "WHERE id = 100";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create an int[] to hold returned values
int[] count = stmt.executeBatch();
//Explicitly commit statements to apply changes
conn.commit();
SQL Exception

Exception is a type of condition when a program encounters a problem in


execution and quits with a problematic error message.

In JDBC, when the program has trouble with a data source, it


throws SQLException.

Note: An SQLException can occur in the JDBC Driver or inside the


database.
Java Database Connectivity (JDBC) serves as the backbone for Java
applications when interacting with databases.

While establishing connections and executing queries, we developers often


encounter SQLExceptions, which are inevitable in the real world.

Handling those exceptions is crucial in the development of applications.


The End

You might also like