SlideShare a Scribd company logo
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 −
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

Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5
 
Java beans
Java beansJava beans
Java beans
Rajkiran Mummadi
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor
 
Servlets
ServletsServlets
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
Muthukumaran Subramanian
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
Rizwan Ali
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
Vikas Jagtap
 
Web api
Web apiWeb api
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
DrSonali Vyas
 
Spring boot
Spring bootSpring boot
Spring boot
Pradeep Shanmugam
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 

What's hot (20)

Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Java beans
Java beansJava beans
Java beans
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Ajax
AjaxAjax
Ajax
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Servlets
ServletsServlets
Servlets
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Java servlets
Java servletsJava servlets
Java servlets
 
JDBC
JDBCJDBC
JDBC
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Web api
Web apiWeb api
Web api
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Spring boot
Spring bootSpring boot
Spring boot
 
Servlets
ServletsServlets
Servlets
 
Servlets
ServletsServlets
Servlets
 

Viewers also liked

Xml dtd
Xml dtdXml dtd
Xml dtd
sana mateen
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Nuha Noor
 
Dom parser
Dom parserDom parser
Dom parser
sana mateen
 
Xml schema
Xml schemaXml schema
Xml schema
sana mateen
 
Xml dom
Xml domXml dom
Xml dom
sana mateen
 
Intro xml
Intro xmlIntro xml
Intro xml
sana mateen
 
Xhtml
XhtmlXhtml
Events1
Events1Events1
Events1
Nuha Noor
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
Nuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
Nuha Noor
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 

Viewers also liked (12)

Xml dtd
Xml dtdXml dtd
Xml dtd
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Dom parser
Dom parserDom parser
Dom parser
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xml dom
Xml domXml dom
Xml dom
 
Intro xml
Intro xmlIntro xml
Intro xml
 
Xhtml
XhtmlXhtml
Xhtml
 
Events1
Events1Events1
Events1
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Reading init param
Reading init paramReading init param
Reading init param
 

Similar to Jdbc in servlets

PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
SivaSankari36
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
Hemo Chella
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Vaishali Modi
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Vaishali Modi
 
java.pptx
java.pptxjava.pptx
java.pptx
bfgd1
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
NaveenKumar648465
 
Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)
Gera Paulos
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
arikazukito
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
saturo3011
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
michaelaaron25322
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
kingkolju
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
Mazenetsolution
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptx
Tanzila Kehkashan
 
Advanced JAVA
Advanced JAVAAdvanced JAVA
Advanced JAVA
Rajvi Vaghasiya
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5
Pallepati Vasavi
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 

Similar to Jdbc in servlets (20)

PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
java.pptx
java.pptxjava.pptx
java.pptx
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
Spring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applicationsSpring data jpa are used to develop spring applications
Spring data jpa are used to develop spring applications
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
OOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptxOOP Lecture 17-DB Connectivity-Part1.pptx
OOP Lecture 17-DB Connectivity-Part1.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
Advanced JAVA
Advanced JAVAAdvanced JAVA
Advanced JAVA
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
 
Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
 

Recently uploaded

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
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
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
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
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
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
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 

Recently uploaded (20)

Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
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
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
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
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 

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 −
  • 3.
  • 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();