SlideShare a Scribd company logo
2
Most read
7
Most read
9
Most read
By
Sana mateen
• What is JDBC Driver?
• JDBC drivers implement the defined interfaces in the JDBC API, for
interacting with your database server.
• JDBC Drivers Types
• JDBC driver implementations vary because of the wide variety of operating
systems and hardware platforms in which Java operates. Sun has divided
the implementation types into four categories, Types 1, 2, 3, and 4, which
is explained below −
Jdbc in servlets
Which Driver should be Used?
1. If you are accessing one type of database, such as Oracle, Sybase, or
IBM, the preferred driver type is 4.
2. If your Java application is accessing multiple types of databases at
the same time, type 3 is the preferred driver.
3. Type 2 drivers are useful in situations, where a type 3 or type 4 driver
is not available yet for your database.
4. The type 1 driver is not considered a deployment-level driver, and is
typically used for development and testing purposes only.
Jdbc(java database connectivity)
• JDBC API is a Java API that can access any kind of tabular data, especially
data stored in a Relational Database. JDBC works with Java on a variety of
platforms, such as Windows, Mac OS, and the various versions of UNIX.
• The JDBC library includes APIs for each of the tasks mentioned below that
are commonly associated with database usage.
• Making a connection to a database.
• Creating SQL or MySQL statements.
• Executing SQL or MySQL queries in the database.
• Viewing & Modifying the resulting records.
• JDBC - Environment Setup:
• Make sure you have done following setup:
• Core JAVA Installation
• SQL or MySQL Database Installation
• Apart from the above you need to setup a database which you would use
for your project.
• Assuming this is EMP and you have created on table Employees within the
same database.
Creating JDBC Application:
• There are six steps involved in building a JDBC application:
1. Import the packages:
• This requires that you include the packages containing the JDBC classes
needed for database programming.
• Most often, using import java.sql.* will suffice as follows:
• //STEP 1. Import required packages
• import java.sql.*;
2. Register the JDBC driver:
• This requires that you initialize a driver so you can open a
communications channel with the database.
• //STEP 2: Register JDBC driver
• Class.forName("com.mysql.jdbc.Driver");
3. Open a connection:
• This requires using the DriverManager.getConnection() method to create
a Connection object, which represents a physical connection with the
database as follows:
• //STEP 3: Open a connection
• // Database credentials
• static final String USER = "username";
• static final String PASS = "password";
• System.out.println("Connecting to database...");
• conn = DriverManager.getConnection(DB_URL,USER,PASS);
4. Execute a query:
• This requires using an object of type Statement or PreparedStatement for
building and submitting an SQL statement to the database as follows:
• //STEP 4: Execute a query
• System.out.println("Creating statement...");
• stmt = conn.createStatement();
• String sql;
• sql = "SELECT id, first, last, age FROM Employees";
• ResultSet rs = stmt.executeQuery(sql);
• If there is an SQL UPDATE,INSERT or DELETE statement required, then
following code snippet would be required:
• //STEP 4: Execute a query
• System.out.println("Creating statement...");
• stmt = conn.createStatement();
• String sql = "DELETE FROM Employees";
• ResultSet rs = stmt.executeUpdate(sql);
5. Extract data from result set:
• This step is required in case you are fetching data from the database. You
can use the appropriate ResultSet.getXXX() method to retrieve the data
from the result set as follows:
• //STEP 5: Extract data from result set
• while(rs.next()){
• //Retrieve by column name
• int id = rs.getInt("id");
• int age = rs.getInt("age");
• String first = rs.getString("first");
• String last = rs.getString("last");
• System.out.print("ID: " + id);
• System.out.print(", Age: " + age);
• System.out.print(", First: " + first);
• System.out.println(", Last: " + last); }
6. Clean up the environment:
• You should explicitly close all database resources versus relying on the
JVM's garbage collection as follows:
• //STEP 6: Clean-up environment
• rs.close();
• stmt.close();
• conn.close();

More Related Content

What's hot (20)

PPT
Network programming in Java
Tushar B Kute
 
PPTX
Reactjs
Neha Sharma
 
PPTX
Introduction to MERN
ShyamMohanKunwar
 
PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PPT
Introduction to .NET Framework
Raghuveer Guthikonda
 
PPT
SQLITE Android
Sourabh Sahu
 
PPTX
Java Database Connectivity (JDBC)
Pooja Talreja
 
PPT
Introduction to JavaScript
Andres Baravalle
 
PPT
React js
Jai Santhosh
 
PPTX
Introduction to React JS
Arnold Asllani
 
PPTX
3 tier architecture
tahir khan
 
PPTX
Servlets
ZainabNoorGul
 
PPTX
React js
Oswald Campesato
 
PPT
JDBC – Java Database Connectivity
Information Technology
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
jQuery
Jay Poojara
 
PPSX
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Network programming in Java
Tushar B Kute
 
Reactjs
Neha Sharma
 
Introduction to MERN
ShyamMohanKunwar
 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
Introduction to .NET Framework
Raghuveer Guthikonda
 
SQLITE Android
Sourabh Sahu
 
Java Database Connectivity (JDBC)
Pooja Talreja
 
Introduction to JavaScript
Andres Baravalle
 
React js
Jai Santhosh
 
Introduction to React JS
Arnold Asllani
 
3 tier architecture
tahir khan
 
Servlets
ZainabNoorGul
 
JDBC – Java Database Connectivity
Information Technology
 
ReactJS presentation.pptx
DivyanshGupta922023
 
jQuery
Jay Poojara
 
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 

Viewers also liked (13)

PPTX
Xml dtd
sana mateen
 
PPT
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Nuha Noor
 
PPTX
Dom parser
sana mateen
 
PPTX
Xml schema
sana mateen
 
PPTX
Http request and http response
Nuha Noor
 
PPTX
Xml dom
sana mateen
 
PPTX
Intro xml
sana mateen
 
PPTX
Xhtml
sana mateen
 
PPT
Events1
Nuha Noor
 
PPT
Understanding layout managers
Nuha Noor
 
PPTX
Using cookies and sessions
Nuha Noor
 
PPTX
Jsp elements
Nuha Noor
 
PPTX
Reading init param
Nuha Noor
 
Xml dtd
sana mateen
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Nuha Noor
 
Dom parser
sana mateen
 
Xml schema
sana mateen
 
Http request and http response
Nuha Noor
 
Xml dom
sana mateen
 
Intro xml
sana mateen
 
Events1
Nuha Noor
 
Understanding layout managers
Nuha Noor
 
Using cookies and sessions
Nuha Noor
 
Jsp elements
Nuha Noor
 
Reading init param
Nuha Noor
 
Ad

Similar to Jdbc in servlets (20)

PPTX
PROGRAMMING IN JAVA -unit 5 -part I
SivaSankari36
 
PPT
Chap3 3 12
Hemo Chella
 
PPT
Java database connectivity
Vaishali Modi
 
PPT
Java database connectivity
Vaishali Modi
 
PPTX
java.pptx
bfgd1
 
PPT
4-INTERDUCATION TO JDBC-2019.ppt
NaveenKumar648465
 
PDF
Java Database Connectivity (Advanced programming)
Gera Paulos
 
PPT
Jdbc connectivity
arikazukito
 
PDF
4_59788783hhhhhhhhhhhhhhhhhhhhhhhhhhhhh34715564451.pdf
kassyemariyam21
 
PDF
Unit 5.pdf
saturo3011
 
PPTX
Spring data jpa are used to develop spring applications
michaelaaron25322
 
PPT
JDBC java for learning java for learn.ppt
kingkolju
 
PPTX
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
deepakdinkar242
 
PPTX
Jdbc
Yamuna Devi
 
PPTX
Java- JDBC- Mazenet Solution
Mazenetsolution
 
PPTX
OOP Lecture 17-DB Connectivity-Part1.pptx
Tanzila Kehkashan
 
PPT
unit-v-17071204384654656646455455448.ppt
AnmolMogalai
 
PPTX
JAVA DATABASE CONNECTIVITY(JDBC CONNECTIVITY).pptx
JGEETHAPRIYA
 
PPTX
Module 3_CSE3146-Advanced Java Programming-JDBC-PPTs.pptx
aruthras2323
 
PROGRAMMING IN JAVA -unit 5 -part I
SivaSankari36
 
Chap3 3 12
Hemo Chella
 
Java database connectivity
Vaishali Modi
 
Java database connectivity
Vaishali Modi
 
java.pptx
bfgd1
 
4-INTERDUCATION TO JDBC-2019.ppt
NaveenKumar648465
 
Java Database Connectivity (Advanced programming)
Gera Paulos
 
Jdbc connectivity
arikazukito
 
4_59788783hhhhhhhhhhhhhhhhhhhhhhhhhhhhh34715564451.pdf
kassyemariyam21
 
Unit 5.pdf
saturo3011
 
Spring data jpa are used to develop spring applications
michaelaaron25322
 
JDBC java for learning java for learn.ppt
kingkolju
 
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
deepakdinkar242
 
Java- JDBC- Mazenet Solution
Mazenetsolution
 
OOP Lecture 17-DB Connectivity-Part1.pptx
Tanzila Kehkashan
 
unit-v-17071204384654656646455455448.ppt
AnmolMogalai
 
JAVA DATABASE CONNECTIVITY(JDBC CONNECTIVITY).pptx
JGEETHAPRIYA
 
Module 3_CSE3146-Advanced Java Programming-JDBC-PPTs.pptx
aruthras2323
 
Ad

Recently uploaded (20)

PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
community health nursing question paper 2.pdf
Prince kumar
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 

Jdbc in servlets

  • 2. • What is JDBC Driver? • JDBC drivers implement the defined interfaces in the JDBC API, for interacting with your database server. • JDBC Drivers Types • JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, which is explained below −
  • 4. Which Driver should be Used? 1. If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4. 2. If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver. 3. Type 2 drivers are useful in situations, where a type 3 or type 4 driver is not available yet for your database. 4. The type 1 driver is not considered a deployment-level driver, and is typically used for development and testing purposes only.
  • 5. Jdbc(java database connectivity) • JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
  • 6. • The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage. • Making a connection to a database. • Creating SQL or MySQL statements. • Executing SQL or MySQL queries in the database. • Viewing & Modifying the resulting records. • JDBC - Environment Setup: • Make sure you have done following setup: • Core JAVA Installation • SQL or MySQL Database Installation • Apart from the above you need to setup a database which you would use for your project. • Assuming this is EMP and you have created on table Employees within the same database.
  • 7. Creating JDBC Application: • There are six steps involved in building a JDBC application: 1. Import the packages: • This requires that you include the packages containing the JDBC classes needed for database programming. • Most often, using import java.sql.* will suffice as follows: • //STEP 1. Import required packages • import java.sql.*; 2. Register the JDBC driver: • This requires that you initialize a driver so you can open a communications channel with the database. • //STEP 2: Register JDBC driver • Class.forName("com.mysql.jdbc.Driver");
  • 8. 3. Open a connection: • This requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database as follows: • //STEP 3: Open a connection • // Database credentials • static final String USER = "username"; • static final String PASS = "password"; • System.out.println("Connecting to database..."); • conn = DriverManager.getConnection(DB_URL,USER,PASS);
  • 9. 4. Execute a query: • This requires using an object of type Statement or PreparedStatement for building and submitting an SQL statement to the database as follows: • //STEP 4: Execute a query • System.out.println("Creating statement..."); • stmt = conn.createStatement(); • String sql; • sql = "SELECT id, first, last, age FROM Employees"; • ResultSet rs = stmt.executeQuery(sql); • If there is an SQL UPDATE,INSERT or DELETE statement required, then following code snippet would be required: • //STEP 4: Execute a query • System.out.println("Creating statement..."); • stmt = conn.createStatement(); • String sql = "DELETE FROM Employees"; • ResultSet rs = stmt.executeUpdate(sql);
  • 10. 5. Extract data from result set: • This step is required in case you are fetching data from the database. You can use the appropriate ResultSet.getXXX() method to retrieve the data from the result set as follows: • //STEP 5: Extract data from result set • while(rs.next()){ • //Retrieve by column name • int id = rs.getInt("id"); • int age = rs.getInt("age"); • String first = rs.getString("first"); • String last = rs.getString("last"); • System.out.print("ID: " + id); • System.out.print(", Age: " + age); • System.out.print(", First: " + first); • System.out.println(", Last: " + last); }
  • 11. 6. Clean up the environment: • You should explicitly close all database resources versus relying on the JVM's garbage collection as follows: • //STEP 6: Clean-up environment • rs.close(); • stmt.close(); • conn.close();