2. JDBC Overview
JDBC is an Application Programming Interface (API) that allows a
Java application to access any kind of tabular data, such as a
database.
JDBC allows us to develop Java applications that can:
Connect to a data source, such as a database
Send queries and update statements to the database
Retrieve and process the results received from the database
JDBC enables us to develop data-centric applications that can
access and manipulate data from diverse range of databases.
For this, JDBC API consists of different types of JDBC drivers,
8/22/2023 2
4. What is JDBC Driver?
A JDBC driver is a software component enabling a Java
application to interact with a database server.
For example, using JDBC drivers enable you to open
database connections and to interact with it by sending
SQL or database commands then receiving results with
Java.
There are four types of JDBC drivers to connect with different
types of databases. These are:
Type 1: JDBC-ODBC Bridge Driver
Type 2: JDBC-Native API Driver
Type 3: JDBC-Network-Protocol Driver (middleware driver)
8/22/2023 4
5. Some Popular JDBC Drivers and URL format
8/22/2023 5
RDBMS JDBC Driver Name and DB URL format
MYSQL
Driver Name
com.mysql.jdbc.Driver
Database URL format:
jdbc:mysql//hostname/databaseName
Oracle
Driver Name:
oracle.jdbc.driver.OracleDriver
Database URL format:
jdbc:oracle:thin@hostname:portnumber:databaseName
JavaDB
Driver Name:
org.apache.derby.jdbc.ClientDriver
Database URL format:
jdbc:derby://localhost:1527/databaseName
Access
Driver Name:
sun.jdbc.odbc.JdbcOdbcDriver
Database URL format:
jdbc:odbc:databaseName
6. Steps to Connect Database Using JDBC
To perform deferent operation on data in the database by using JDBC,
it is required to follow the following steps in application
1. Import package
2. Load the JDBC driver
3. Establish the Connection
4. Create a Statement object
5. Execute a query
6. Process the results
7. Close the connection
8/22/2023 6
7. Core Components of JOBC
The core components of JOBC are provided under the java . Sql
package.
These core components includes, but are not limited to:
JDBC Drivers: Allow establishing a communication between a
database and a Java application. It is available as Driver interface in
the java. sql package.
Connections: Allow establishing a connection session with the
database. It is available as the Connection interface in the java. sql
package.
Statements: Allow executing static SQL statements and returning
the results. It is available as the Statement interface in the java .sql
package.
To execute dynamic SQL statements the PreparedStatement
8/22/2023 7
8. Core Components of JOBC…
Result Sets: Allow storing the data retrieved as a result of a SQL
queries in a tabular format. It is available as the Statement
interface in the java.sql package.
Database Meta Data : Allow to get information about the
database as a whole. It is available as DatabaseMetaData
interface in java sql package.
Result Set Meta Data: Allows to get information about the types
and properties of the columns in the ResultSet object. It is
available as the ResultSetMetaData interface in the java sql
package.
8/22/2023 8
10. Execute a query
boolean execute (String sql)
Executes the given SQL statement, which may return multiple results.
Is used in the situations where you are not sure whether the query to
be executed is an insert, update, or delete statement or it is a query to
retrieve data
ResultSet executeQuery(String sql)
Executes the given SQL statement, which returns a single ResultSet
object
Is used to execute the queries that retrieve data from the databse.
int executeUpdate(String sql)
Executes the given SQL statements which may be CREATE, INSERT,
UPDATE, or DELETE statement or an SQL statement that returns
nothing, such as an SQL DDL statement.
8/22/2023 10
11. Example of JDBC using MYSQL Database
Perform CRUD Operation includes
Create/Insert Records
Read/View Record
Update Record
Delete Record
All the above operations including Database and Result Set
Meta Data are preformed in the following example.
8/22/2023 11