SlideShare a Scribd company logo
JDBC – Java Database Connectivity
Session – 1
 What is JDBC?
 JDBC General Architecture
 JDBC API components
 JDBC Drivers,
 JDBC Driver Types
 JDBC Interfaces, Classes
 JDBC Connectivity Steps
 Connectivity with MySQL
 Connection
 CRUD Operations
 Prepared Statement
 Updateable ResultSet
By Jainul Musani
JDBC – Java Database Connectivity
What is JDBC ?
 To connect Java Applications with Database.
 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.
By Jainul Musani
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
JDBC API components
 The java.sql package contains classes and interfaces for
JDBC API.
 JDBC Drivers
 JDBC Interfaces
 JDBC Classes
By Jainul Musani
JDBC – Java Database Connectivity
JDBC Drivers
There are four types of JDBC drivers:
1) JDBC-ODBC Bridge Driver,
2) Native Driver,
3) Network Protocol Driver, and
4) Thin Driver
By Jainul Musani
JDBC – Java Database Connectivity
1) Driver interface
2) Connection interface
3) Statement interface
4) PreparedStatement
interface
5) CallableStatement
interface
6) ResultSet interface
7) ResultSetMetaData
interface
8) DatabaseMetaData
interface
9) RowSet interface
By Jainul Musani
JDBC Interfaces
There are Nine types of JDBC Interfaces:
JDBC – Java Database Connectivity
 DriverManager class
 Blob class
 Clob class
 Types class
By Jainul Musani
JDBC Classes
JDBC – Java Database Connectivity
JDBC Drivers : JDBC-ODBC Bridge Driver
By Jainul Musani
 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.
JDBC – Java Database Connectivity
JDBC Drivers : 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.
By Jainul Musani
JDBC – Java Database Connectivity
JDBC Drivers : Network Protocol Driver
By Jainul Musani
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: No client side library is
required
Disadvantages:
Network support is required on client
machine.
Requires database-specific coding to
be done in the middle tier.
JDBC – Java Database Connectivity
JDBC Drivers : Thin Driver
By Jainul Musani
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:
Better performance than all other
drivers.
No software is required at client
side or server side.
Disadvantage:
Drivers depend on the Database.
JDBC – Java Database Connectivity
1) Register the Driver class
2) Create connection
3) Create statement
4) Execute queries
5) Close connection
By Jainul Musani
Java Database Connectivity Steps:
JDBC – Java Database Connectivity
Oracle: download the jar file ojdbc14.jar
MySQL: download the jar file mysql-connector.jar
Two ways to load the jar file:
1) paste the ojdbc14.jar file in jre/lib/ext folder
2) set classpath
https://www.soapui.org/docs/jdbc/reference/jdbc-drivers/
By Jainul Musani
Download and Setup the drivers
JDBC – Java Database Connectivity
The forName() method of Class class is used to register the
driver class.
This method is used to dynamically load the driver class.
Syntax:
public static void forName(String className) throws
ClassNotFoundException
By Jainul Musani
Step:1: Register the Driver Class
JDBC – Java Database Connectivity
Example:
Loading Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Loading MySQL Driver
Class.forName("com.mysql.jdbc.Driver");
https://www.soapui.org/docs/jdbc/reference/jdbc-drivers/
By Jainul Musani
Step:1: Register the Driver Class
JDBC – Java Database Connectivity
try{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
System.out.println("MySQL JDBC Driver Registered!");
By Jainul Musani
Step:1: Register the Driver Class
JDBC – Java Database Connectivity
The getConnection() method of DriverManager class is used
to establish connection with the database.
Syntax:
public static Connection getConnection(String url) throws
SQLException
public static Connection getConnection(String url, String
name, String password) throws SQLException
By Jainul Musani
Step:2: Create connection Object
JDBC – Java Database Connectivity
Connection connection = null;
try {
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBCDemo",
"root", "password");
System.out.println("SQL Connection to database established!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
return;
}
By Jainul Musani
Step:2: Create connection Object
JDBC – Java Database Connectivity
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");
stmt = con.createStatement();
stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) "
+ "VALUES (1,'Lokesh','Gupta',5)");
}
By Jainul Musani
Step:3: Create statement
JDBC – Java Database Connectivity
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");
stmt = con.createStatement();
stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) "
+ "VALUES (1,'Lokesh','Gupta',5)");
}
By Jainul Musani
Step:4: Execute Queries
JDBC – Java Database Connectivity
try{
:::::::::::::
}catch (Exception e) {
e.printStackTrace();
}finally {
stmt.close();
con.close();
}
By Jainul Musani
Step:5: Close Connection
JDBC – Java Database Connectivity
ResultSet rs =
selectStmt.executeQuery(
“Select StudID, StudName, Subject, Marsk From Students
Where StudID Between 101 And 150");
while(rs.next())
{
System.out.println(rs.getString(1)); //First Column
System.out.println(rs.getString(2)); //Second Column
System.out.println(rs.getString(3)); //Third Column
System.out.println(rs.getString(4)); //Fourth Column
}
By Jainul Musani
Fetch the data from result set
JDBC – Java Database Connectivity
Connection con = null;
PreparedStatement pstmt = null;
String sql = “Insert into Students
(StudID,StudName,Subject,Marks)
VALUES (?,?,?,?)";
pstmt = con.PreparedStatement(sql);
By Jainul Musani
JDBC PreparedStatement
JDBC – Java Database Connectivity
pstmt.setInt(1, 87);
pstmt.setString(2, "Lokesh");
pstmt.setString(3, "Gupta");
pstmt.setInt(4, 5);
int affectedRows = pstmt.executeUpdate();
System.out.println(affectedRows + " row(s) affected !!");
By Jainul Musani
JDBC PreparedStatement

More Related Content

What's hot

Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
Bagzzz
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
Return on Intelligence
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
Venkateswara Rao N
 
Java web application development
Java web application developmentJava web application development
Java web application development
RitikRathaur
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Dynamic storage allocation techniques
Dynamic storage allocation techniquesDynamic storage allocation techniques
Dynamic storage allocation techniquesShashwat Shriparv
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Hash table in java
Hash table in javaHash table in java
Hash table in java
siriindian
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 
Java rmi
Java rmiJava rmi
Java rmi
kamal kotecha
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
2 phase locking protocol DBMS
2 phase locking protocol DBMS2 phase locking protocol DBMS
2 phase locking protocol DBMS
Dhananjaysinh Jhala
 
Textbox n label
Textbox n labelTextbox n label
Textbox n label
chauhankapil
 

What's hot (20)

Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Event handling
Event handlingEvent handling
Event handling
 
Dynamic storage allocation techniques
Dynamic storage allocation techniquesDynamic storage allocation techniques
Dynamic storage allocation techniques
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Hash table in java
Hash table in javaHash table in java
Hash table in java
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Java rmi
Java rmiJava rmi
Java rmi
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
2 phase locking protocol DBMS
2 phase locking protocol DBMS2 phase locking protocol DBMS
2 phase locking protocol DBMS
 
Textbox n label
Textbox n labelTextbox n label
Textbox n label
 

Similar to Fundamentals of JDBC

jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
 jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
ujjwalmatoliya
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
Paneliya Prince
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
Paneliya Prince
 
JDBC : Java Database Connectivity
JDBC : Java Database Connectivity JDBC : Java Database Connectivity
JDBC : Java Database Connectivity
DevAdnani
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
Arumugam90
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
Arumugam90
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
Dhyey Dattani
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
Dhyey Dattani
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
chapter 5 java.pptx
chapter 5  java.pptxchapter 5  java.pptx
chapter 5 java.pptx
BekiTube
 
JDBC Presentation with JAVA code Examples.pdf
JDBC Presentation with JAVA code Examples.pdfJDBC Presentation with JAVA code Examples.pdf
JDBC Presentation with JAVA code Examples.pdf
ssuser8878c1
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
msafad
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
Sourabrata Mukherjee
 
jdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptxjdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptx
ssuser8878c1
 
Jdbc drivers
Jdbc driversJdbc drivers
Jdbc drivers
Saurabh Bhartiya
 
Jdbc new
Jdbc newJdbc new
Jdbc new
sumit kushwah
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
Mythili Shankar
 

Similar to Fundamentals of JDBC (20)

jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
 jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
 
JDBC : Java Database Connectivity
JDBC : Java Database Connectivity JDBC : Java Database Connectivity
JDBC : Java Database Connectivity
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
chapter 5 java.pptx
chapter 5  java.pptxchapter 5  java.pptx
chapter 5 java.pptx
 
JDBC Presentation with JAVA code Examples.pdf
JDBC Presentation with JAVA code Examples.pdfJDBC Presentation with JAVA code Examples.pdf
JDBC Presentation with JAVA code Examples.pdf
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
jdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptxjdbc-130913021409-phpapp01000988www.pptx
jdbc-130913021409-phpapp01000988www.pptx
 
JDBC
JDBCJDBC
JDBC
 
Jdbc drivers
Jdbc driversJdbc drivers
Jdbc drivers
 
jdbc
jdbcjdbc
jdbc
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
 

More from Jainul Musani

Python: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - IntroductionPython: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - Introduction
Jainul Musani
 
Python a Versatile Programming Language - Introduction
Python a Versatile Programming Language - IntroductionPython a Versatile Programming Language - Introduction
Python a Versatile Programming Language - Introduction
Jainul Musani
 
React js t8 - inlinecss
React js   t8 - inlinecssReact js   t8 - inlinecss
React js t8 - inlinecss
Jainul Musani
 
React js t7 - forms-events
React js   t7 - forms-eventsReact js   t7 - forms-events
React js t7 - forms-events
Jainul Musani
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
Jainul Musani
 
React js t5 - state
React js   t5 - stateReact js   t5 - state
React js t5 - state
Jainul Musani
 
React js t4 - components
React js   t4 - componentsReact js   t4 - components
React js t4 - components
Jainul Musani
 
React js t3 - es6
React js   t3 - es6React js   t3 - es6
React js t3 - es6
Jainul Musani
 
React js t2 - jsx
React js   t2 - jsxReact js   t2 - jsx
React js t2 - jsx
Jainul Musani
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introduction
Jainul Musani
 
ExpressJs Session01
ExpressJs Session01ExpressJs Session01
ExpressJs Session01
Jainul Musani
 
NodeJs Session03
NodeJs Session03NodeJs Session03
NodeJs Session03
Jainul Musani
 
NodeJs Session02
NodeJs Session02NodeJs Session02
NodeJs Session02
Jainul Musani
 
Nodejs Session01
Nodejs Session01Nodejs Session01
Nodejs Session01
Jainul Musani
 
Java exercise1
Java exercise1Java exercise1
Java exercise1
Jainul Musani
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
Jainul Musani
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
Jainul Musani
 
Cassandra-vs-MongoDB
Cassandra-vs-MongoDBCassandra-vs-MongoDB
Cassandra-vs-MongoDB
Jainul Musani
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
Jainul Musani
 
MongoDB-SESSION02
MongoDB-SESSION02MongoDB-SESSION02
MongoDB-SESSION02
Jainul Musani
 

More from Jainul Musani (20)

Python: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - IntroductionPython: The Versatile Programming Language - Introduction
Python: The Versatile Programming Language - Introduction
 
Python a Versatile Programming Language - Introduction
Python a Versatile Programming Language - IntroductionPython a Versatile Programming Language - Introduction
Python a Versatile Programming Language - Introduction
 
React js t8 - inlinecss
React js   t8 - inlinecssReact js   t8 - inlinecss
React js t8 - inlinecss
 
React js t7 - forms-events
React js   t7 - forms-eventsReact js   t7 - forms-events
React js t7 - forms-events
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
 
React js t5 - state
React js   t5 - stateReact js   t5 - state
React js t5 - state
 
React js t4 - components
React js   t4 - componentsReact js   t4 - components
React js t4 - components
 
React js t3 - es6
React js   t3 - es6React js   t3 - es6
React js t3 - es6
 
React js t2 - jsx
React js   t2 - jsxReact js   t2 - jsx
React js t2 - jsx
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introduction
 
ExpressJs Session01
ExpressJs Session01ExpressJs Session01
ExpressJs Session01
 
NodeJs Session03
NodeJs Session03NodeJs Session03
NodeJs Session03
 
NodeJs Session02
NodeJs Session02NodeJs Session02
NodeJs Session02
 
Nodejs Session01
Nodejs Session01Nodejs Session01
Nodejs Session01
 
Java exercise1
Java exercise1Java exercise1
Java exercise1
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
 
Core Java Special
Core Java SpecialCore Java Special
Core Java Special
 
Cassandra-vs-MongoDB
Cassandra-vs-MongoDBCassandra-vs-MongoDB
Cassandra-vs-MongoDB
 
MongoDB-SESSION03
MongoDB-SESSION03MongoDB-SESSION03
MongoDB-SESSION03
 
MongoDB-SESSION02
MongoDB-SESSION02MongoDB-SESSION02
MongoDB-SESSION02
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Fundamentals of JDBC

  • 1. JDBC – Java Database Connectivity Session – 1  What is JDBC?  JDBC General Architecture  JDBC API components  JDBC Drivers,  JDBC Driver Types  JDBC Interfaces, Classes  JDBC Connectivity Steps  Connectivity with MySQL  Connection  CRUD Operations  Prepared Statement  Updateable ResultSet By Jainul Musani
  • 2. JDBC – Java Database Connectivity What is JDBC ?  To connect Java Applications with Database.  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. By Jainul Musani
  • 3. JDBC – Java Database Connectivity
  • 4. JDBC – Java Database Connectivity
  • 5. JDBC – Java Database Connectivity
  • 6. JDBC – Java Database Connectivity JDBC API components  The java.sql package contains classes and interfaces for JDBC API.  JDBC Drivers  JDBC Interfaces  JDBC Classes By Jainul Musani
  • 7. JDBC – Java Database Connectivity JDBC Drivers There are four types of JDBC drivers: 1) JDBC-ODBC Bridge Driver, 2) Native Driver, 3) Network Protocol Driver, and 4) Thin Driver By Jainul Musani
  • 8. JDBC – Java Database Connectivity 1) Driver interface 2) Connection interface 3) Statement interface 4) PreparedStatement interface 5) CallableStatement interface 6) ResultSet interface 7) ResultSetMetaData interface 8) DatabaseMetaData interface 9) RowSet interface By Jainul Musani JDBC Interfaces There are Nine types of JDBC Interfaces:
  • 9. JDBC – Java Database Connectivity  DriverManager class  Blob class  Clob class  Types class By Jainul Musani JDBC Classes
  • 10. JDBC – Java Database Connectivity JDBC Drivers : JDBC-ODBC Bridge Driver By Jainul Musani  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.
  • 11. JDBC – Java Database Connectivity JDBC Drivers : 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. By Jainul Musani
  • 12. JDBC – Java Database Connectivity JDBC Drivers : Network Protocol Driver By Jainul Musani 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: No client side library is required Disadvantages: Network support is required on client machine. Requires database-specific coding to be done in the middle tier.
  • 13. JDBC – Java Database Connectivity JDBC Drivers : Thin Driver By Jainul Musani 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: Better performance than all other drivers. No software is required at client side or server side. Disadvantage: Drivers depend on the Database.
  • 14. JDBC – Java Database Connectivity 1) Register the Driver class 2) Create connection 3) Create statement 4) Execute queries 5) Close connection By Jainul Musani Java Database Connectivity Steps:
  • 15. JDBC – Java Database Connectivity Oracle: download the jar file ojdbc14.jar MySQL: download the jar file mysql-connector.jar Two ways to load the jar file: 1) paste the ojdbc14.jar file in jre/lib/ext folder 2) set classpath https://www.soapui.org/docs/jdbc/reference/jdbc-drivers/ By Jainul Musani Download and Setup the drivers
  • 16. JDBC – Java Database Connectivity The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. Syntax: public static void forName(String className) throws ClassNotFoundException By Jainul Musani Step:1: Register the Driver Class
  • 17. JDBC – Java Database Connectivity Example: Loading Oracle Driver Class.forName("oracle.jdbc.driver.OracleDriver"); Loading MySQL Driver Class.forName("com.mysql.jdbc.Driver"); https://www.soapui.org/docs/jdbc/reference/jdbc-drivers/ By Jainul Musani Step:1: Register the Driver Class
  • 18. JDBC – Java Database Connectivity try{ Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("MySQL JDBC Driver not found !!"); return; } System.out.println("MySQL JDBC Driver Registered!"); By Jainul Musani Step:1: Register the Driver Class
  • 19. JDBC – Java Database Connectivity The getConnection() method of DriverManager class is used to establish connection with the database. Syntax: public static Connection getConnection(String url) throws SQLException public static Connection getConnection(String url, String name, String password) throws SQLException By Jainul Musani Step:2: Create connection Object
  • 20. JDBC – Java Database Connectivity Connection connection = null; try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password"); System.out.println("SQL Connection to database established!"); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); return; } By Jainul Musani Step:2: Create connection Object
  • 21. JDBC – Java Database Connectivity Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password"); stmt = con.createStatement(); stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) " + "VALUES (1,'Lokesh','Gupta',5)"); } By Jainul Musani Step:3: Create statement
  • 22. JDBC – Java Database Connectivity Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password"); stmt = con.createStatement(); stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) " + "VALUES (1,'Lokesh','Gupta',5)"); } By Jainul Musani Step:4: Execute Queries
  • 23. JDBC – Java Database Connectivity try{ ::::::::::::: }catch (Exception e) { e.printStackTrace(); }finally { stmt.close(); con.close(); } By Jainul Musani Step:5: Close Connection
  • 24. JDBC – Java Database Connectivity ResultSet rs = selectStmt.executeQuery( “Select StudID, StudName, Subject, Marsk From Students Where StudID Between 101 And 150"); while(rs.next()) { System.out.println(rs.getString(1)); //First Column System.out.println(rs.getString(2)); //Second Column System.out.println(rs.getString(3)); //Third Column System.out.println(rs.getString(4)); //Fourth Column } By Jainul Musani Fetch the data from result set
  • 25. JDBC – Java Database Connectivity Connection con = null; PreparedStatement pstmt = null; String sql = “Insert into Students (StudID,StudName,Subject,Marks) VALUES (?,?,?,?)"; pstmt = con.PreparedStatement(sql); By Jainul Musani JDBC PreparedStatement
  • 26. JDBC – Java Database Connectivity pstmt.setInt(1, 87); pstmt.setString(2, "Lokesh"); pstmt.setString(3, "Gupta"); pstmt.setInt(4, 5); int affectedRows = pstmt.executeUpdate(); System.out.println(affectedRows + " row(s) affected !!"); By Jainul Musani JDBC PreparedStatement