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

Introduction to Java Database Connectivity JDBC

JDBC (Java Database Connectivity) is a Java API that facilitates database connections, query execution, and result handling. It operates through a two-layer interface, connecting Java applications to databases via JDBC drivers. The process of using JDBC involves establishing a connection, creating and executing statements, processing results, and closing connections properly.

Uploaded by

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

Introduction to Java Database Connectivity JDBC

JDBC (Java Database Connectivity) is a Java API that facilitates database connections, query execution, and result handling. It operates through a two-layer interface, connecting Java applications to databases via JDBC drivers. The process of using JDBC involves establishing a connection, creating and executing statements, processing results, and closing connections properly.

Uploaded by

Kamal T
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Processing SQL Statements with JDBC

• What is JDBC (Java Database Connectivity)


• How JDBC works
• Finding a JDBC driver
• The steps for connecting to a database with JDBC
What is JDBC (Java Database Connectivity)

• JDBC (Java Database Connectivity) is the Java API that


manages connecting to a database, issuing queries and
commands, and handling result sets obtained from the
database.
• Released as part of JDK 1.1 in 1997, JDBC was one of the
first components developed for the Java persistence layer.
How JDBC works

• Developed as an alternative to the C-based ODBC (Open Database


Connectivity) API, JDBC offers a programming-level interface that handles
the mechanics of Java applications communicating with a database or
RDBMS.
• The JDBC interface consists of two layers:
1.The JDBC API supports communication between the Java application and the
JDBC manager.
2.The JDBC driver supports communication between the JDBC manager and
the database driver.
• JDBC is the common API that your application code interacts with.
• Beneath that is the JDBC-compliant driver for the database you are using.
Finding a JDBC driver

• To find a driver for the database you want to use, simply do a web search for your database and JDBC.
• For instance, typing in "mysql jdbc driver" will turn up a driver for MySQL
The steps for connecting to a
database with JDBC

In general, to process any SQL statement with JDBC, you follow these
steps:
1-Establishing a connection.
2-Create a statement.
3-Execute the query.
4-Process the ResultSet object.
5-Close the connection.
1-Establishing a connection
2-Create a statement.
2-Create a statement. Cont

• There are three different kinds of statements:


• Statement: Used to implement simple SQL statements with no parameters.
• PreparedStatement: (Extends Statement.) Used for precompiling SQL
statements that might contain input parameters
• CallableStatement: (Extends PreparedStatement.) Used to execute stored
procedures that may contain both input and output parameters
3-Executing Queries

• To execute a query, call an execute method from Statement such as the following:
• execute: Returns true if the first object that the query returns is a ResultSet object. Use this method if
the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from
the query by repeatedly calling Statement.getResultSet.
• executeQuery: Returns one ResultSet object.
• executeUpdate: Returns an integer representing the number of rows affected by the SQL statement.
Use this method if you are using INSERT, DELETE, or UPDATE SQL statements.
• For example,
• CoffeesTables.viewTable executed a Statement object with the following code:
• ResultSet rs = stmt.executeQuery(query);
4-Process the ResultSet object.
5-Close the connection.

When you are finished using a Connection, Statement, or ResultSet object, call its close
method to immediately release the resources it's using.

Alternatively, use a try-with-resources statement to automatically close Connection,


Statement, and ResultSet objects, regardless of whether an SQLException has been thrown.
(JDBC throws an SQLException when it encounters an error during an interaction with a data
source
5-Close the • An automatic resource statement consists of a try
statement and one or more declared resources. For
connection. example, the CoffeesTables.viewTable method automatically
Cont closes its Statement object, as follows:

You might also like