0% found this document useful (0 votes)
5 views12 pages

JDBC

The document provides an overview of Java Database Connectivity (JDBC), including its purpose, types of JDBC drivers, and the JDBC API. It outlines the steps to connect a Java application to a MySQL database, perform CRUD operations, and manage database connections. Additionally, it discusses the Data Access Object (DAO) pattern for abstracting data access in applications.

Uploaded by

Ashwini
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)
5 views12 pages

JDBC

The document provides an overview of Java Database Connectivity (JDBC), including its purpose, types of JDBC drivers, and the JDBC API. It outlines the steps to connect a Java application to a MySQL database, perform CRUD operations, and manage database connections. Additionally, it discusses the Data Access Object (DAO) pattern for abstracting data access in applications.

Uploaded by

Ashwini
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/ 12

JDBC

Java Database Connectivity

Topics Covered:-
• JDBC
• Why we should we use JDBC
• JDBC Drivers
• JDBC API
• Java Database Connectivity with MySQL
• Steps to Connect with Database
• Creating Connection with database (JDBC)
• Creating Table (CRUD)
• Inserting values into table (CRUD)
• Dynamic Values inserted
• Updating data using the dynamic values using BufferedReader
• Fetching details from database (CRUD)
• Data Access Object in JDBC - (DAO classes)

Uday Sharma
[email protected]
JDBC
• JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query
with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to
connect with the database.
There are four types of JDBC drivers:
• JDBC-ODBC Bridge Driver,
• Native Driver,
• Network Protocol Driver, and
• Thin Driver
• JDBC API:-

A list of popular classes of JDBC API are given below:


o DriverManager class
o Blob class
o Clob class
• Types class
• Why Should We Use JDBC:-
Before JDBC, ODBC API was the database API to connect and execute the query with the database.
But ODBC API uses ODBC driver which is written in C language (i.e., platform dependent and
unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written
in Java language).
We can use JDBC API to handle database using Java program and can perform the following
activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
• What is API:-
API (Application programming interface) is a document that contains a description of all the
features of a product or software. It represents classes and interfaces that software programs
can follow to communicate with each other. An API can be created for applications, libraries,
operating systems, etc.
• JDBC Driver:-
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
1. JDBC-ODBC bridge driver:-
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge
driver converts JDBC method calls into the ODBC function calls. This is now discouraged because
of thin driver.

In Java 8, the JDBC-ODBC Bridge has been removed.


Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that you use
JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.
Advantages:-
o easy to use.
o can be easily connected to any database.
Disadvantages:-
o Performance degraded because JDBC method call is converted into the ODBC function
calls.
o The ODBC driver needs to be installed on the client machine.
2. Native-API driver:-
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.

Advantage:-
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:-
o The Native driver needs to be installed on the each client machine.
o The Vendor client library needs to be installed on client machine.
3. Network Protocol driver:-
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.

Advantage:-
o No client side library is required because of application server that can perform many
tasks like auditing, load balancing, logging etc.
Disadvantages:-
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires database-
specific coding to be done in the middle tier.
4. Thin driver:-
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.

Advantage:-
o Better performance than all other drivers.
o No software is required at client side or server side.
Disadvantage:-
o Drivers depend on the Database.
• JDBC Connectivity Steps:-
There are 5 steps to connect any java application with the database using JDBC. These steps are
as follows:-
1. Import the package.
2. Load & register the driver.
3. Establish the connection .
4. Create the statement.
5. Execute the query.
6. Process result.
7. Close connection .
• Steps to Connect with Database,
explained in Detail
(Java Database Connectivity) JDBC:-
Steps to connect java program with database-
1) load the driver:-
Class.forName("com.mysql.jdbc.Driver")//(inside of try-catch )
Or
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
2)Create a Connection:-
Connection con=DriverManager.getConnection("url",”Username”,”Password”);
Connection
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbname","root","root");
3)Create query, Statement , PreparedStatement , CallableStatement:-
String q="select * from students";
Statement stmt=con.createStatement(); // stored in the stmt
ResultSet set=stmt.executeQuery(q); // fire with the help of stmt
4)Process the data :-
while(set.next()) // its help to move into next row
{
int id=set.getInt("studentID"); // we can also pass the column name
String name=set.getString("studentName");
System.out.println(id);
System.out.println(name);
}
5) Close the connection:-
st.close();
con.close();
Remember Note:
“If you want to fetch the data from the database you have to use .executeQuery() method and if
want to update or perform the curd operation you have to use .executeUpdate() method.”
• MySQL-Connector Jar File:-
• For using the database you have to download the MySQL jar file for java EE version.
And built the Classpath of that jar file where you using the Java Project with MySQL.
• Following steps To build the path of jar File in JAVA EE.
• I name my java-project as JDBC:-
• Java Database Connectivity with MySQL:-
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations
for the mysql database:
1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database,
localhost is the server name on which mysql is running, we may also use IP address,
3306 is the port number and sonoo is the database name. We may use any database, in
such case, we need to replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
• Connection interface:-
• A Connection is a session between a Java application and a database. It helps to establish a
connection with the database.
• The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData, i.e., an 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(), setAutoCommit(), setTransactionIsolation(), etc.
By default, connection commits the changes after executing queries.
• Commonly used methods of Connection interface:-
1) 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.
3) public void setAutoCommit(boolean status): is used to set the commit status. By default, it
is true.
4) public void commit(): saves the changes made since the previous commit/rollback is
permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.
• Connection Interface Fields:-
There are some common Connection interface constant fields that are present in the Connect
interface. These fields specify the isolation level of a transaction.
• TRANSACTION_NONE: No transaction is supported, and it is indicated by this constant.
• TRANSACTION_READ_COMMITTED: It is a constant which shows that the dirty reads are not
allowed. However, phantom reads and non-repeatable reads can occur.
• TRANSACTION_READ_UNCOMMITTED: It is a constant which shows that dirty reads, non-
repeatable reads, and phantom reads can occur.
• TRANSACTION_REPEATABLE_READ: It is a constant which shows that the non-repeatable
reads and dirty reads are not allowed. However, phantom reads and can occur.
• TRANSACTION_SERIALIZABLE: It is a constant which shows that the non-repeatable reads,
dirty reads as well as the phantom reads are not allowed.
• Creating Connection with database (JDBC)
Console:-

• Creating Table (CRUD):-


• Create table Operation:-

Database is create we can see in the mysqlcommandline client. Or we can in MySQL


Workbench
• Inserting values into table (CRUD):-

Output:- on Console

Output:- on MySQL Workbench


• Dynamic Values inserted;
• Inserting values into table by user input using BufferedReader class (CRUD):-

Output:- On Console:-

Output:- MySQL CommandLine :-


• Updating data using the dynamic values using BufferedReader:-

Output:-
Before Update Query

After Update Query


• Fetching details from database (CRUD):-

Output:-

• Data Access Object in JDBC - (DAO classes)


DAO is an abstraction for accessing data, the idea is to separate the technical details of data
access from the rest of the application. It can apply to any kind of data. JDBC is an API for
accessing relational databases using Java.
• Topics Covered in the NOTES :-

Eclipse IDE , JAVA EE , JDK-18

➢ Go check out my LinkedIn profile for more notes and other resources content
https://www.linkedin.com/in/uday-sharma-602b33267

You might also like