SlideShare a Scribd company logo
1 of 14
Introduction to JDBC
Michelle Lee, Ye Wu & Jeff Offutt
http://www.cs.gmu.edu/~offutt/
SWE 432
Design and Implementation of Software for the Web
3/19/2024 © Wu, Lee & Offutt 2
JDBC
• JDBC (Java Database Connectivity) API allows Java
programs to connect to databases
• Database access is the same for all database vendors
• The JVM uses a JDBC driver to translate generalized
JDBC calls into vendor specific database calls
• There are four general types of JDBC drivers
– We will look at Type 4 …
3/19/2024 © Wu, Lee & Offutt 3
Pure Java Driver (Type 4)
• These drivers convert the JDBC API calls to direct
network calls using vendor-specific networking protocols
by making direct socket connections with the database
• It is the most efficient method to access database, both in
performance and development time
• It is the simplest to deploy
• All major database vendors provide pure Java JDBC
drivers for their databases and they are also available from
third party vendors
• For a list of JDBC drivers, refer to
– http://industry.java.sun.com/products/jdbc/drivers
3/19/2024 © Wu, Lee & Offutt 4
Java
Application
DB Client
Pure Java Driver (2)
JDBC
API
JDBC Driver
Data Source
Server
3/19/2024 © Wu, Lee & Offutt 5
Typical JDBC Programming Procedure
1. Load the database driver
2. Obtain a connection
3. Create and execute statements (SQL queries)
4. Use result sets (tables) to navigate through the results
5. Close the connection
3/19/2024 © Wu, Lee & Offutt 6
Driver Manager
• The purpose of the java.sql.DriverManger class in
JDBC is to provide a common access layer on top of
different database drivers used in an application
• DriverManager requires that each driver required by the
application must be registered before use, so that the
DriverManager is aware of it
• Load the database driver using ClassLoader :
– Class.forName (“oracle.jdbc.driver.OracleDriver”);
3/19/2024 © Wu, Lee & Offutt 7
Connecting to a Database
• Type 4 JDBC Driver – Oracle Server
Class.forName (“oracle.jdbc.driver.OracleDriver”);
con = DriverManager.getConnection (
“jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite”,
“accountname", “password”);
• Type 4 JDBC Driver – MySQL Server
Class.forName (“org.gjt.mm.mysql.Driver”);
con = DriverManager.getConnection
(“jdbc:mysql://localhost/databasename”, uid, passwd);
3/19/2024 © Wu, Lee & Offutt 8
Creating Tables
• Creating a Coffee table
CREATE TABLE COFFEES (COF_NAME VARCHAR(32), SUP_ID
INTEGER, PRICE FLOAT, SALES INTEGER, TOTAL INTEGER)
• Creating JDBC statements
Statement stmt = con.createStatement ();
• Execute a statement
stmt.executeUpdate (“CREATE TABLE COFFEES “ +
“(COF_NAME VARCHAR(32), SUP_ID INTEGER,
PRICE FLOAT, “ + “SALES INTEGER, TOTAL
INTEGER)”);
SQL query
3/19/2024 © Wu, Lee & Offutt 9
Execute Statements
• This uses executeUpdate because the SQL statement
contained in createTableCoffees is a DDL (data
definition language) statement
• Statements that create a table, alter a table, or drop a table
are all examples of DDL statements and are executed with
the method executeUpdate
• executeUpdate is also used to execute SQL statements
that update a table
3/19/2024 © Wu, Lee & Offutt 10
Execute Statements
• In practice, executeUpdate is used far more often to
update tables than it is to create them because a table is
created once but may be updated many times
• The method used most often for executing SQL statements
is executeQuery
• executeQuery is used to execute SELECT statements,
which comprise the vast majority of SQL statements
3/19/2024 © Wu, Lee & Offutt 11
Entering Data into a Table
Statement stmt = con.createStatement();
stmt.executeUpdate ( "INSERT INTO COFFEES " +
"VALUES ('Colombian', 101, 7.99, 0, 0)");
stmt.executeUpdate ( "INSERT INTO COFFEES " +
"VALUES ('French_Roast', 49, 8.99, 0, 0)" );
stmt.executeUpdate ( "INSERT INTO COFFEES " +
"VALUES ('Espresso', 150, 9.99, 0, 0)" );
stmt.executeUpdate ( "INSERT INTO COFFEES " +
"VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)" );
stmt.executeUpdate ( "INSERT INTO COFFEES " +
"VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)" );
3/19/2024 © Wu, Lee & Offutt 12
Getting Data From a Table
ResultSet rs = stmt.executeQuery ("SELECT COF_NAME, PRICE
FROM COFFEES");
while (rs.next())
{
String s = rs.getString ("COF_NAME");
float n = rs.getFloat ("PRICE");
System.out.println (s + " " + n);
}
3/19/2024 © Wu, Lee & Offutt 13
JNDI
Connection Manager
JDBC Data Source Architecture
Application JDBC Database
3/19/2024 © Wu, Lee & Offutt 14
Sample code
• Sun JDBC tutorial

More Related Content

Similar to JDBC.ppt database connectivity in java ppt

Similar to JDBC.ppt database connectivity in java ppt (20)

Prashanthi
PrashanthiPrashanthi
Prashanthi
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
jdbc
jdbcjdbc
jdbc
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
JavaFX Enterprise (JavaOne 2014)
JavaFX Enterprise (JavaOne 2014)JavaFX Enterprise (JavaOne 2014)
JavaFX Enterprise (JavaOne 2014)
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
Jdbc
JdbcJdbc
Jdbc
 
PROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part IIPROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part II
 

More from kavitamittal18 (17)

Lec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreadingLec7!JavaThreads.ppt java multithreading
Lec7!JavaThreads.ppt java multithreading
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture notes
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing ppt
 
AdHocTutorial.ppt
AdHocTutorial.pptAdHocTutorial.ppt
AdHocTutorial.ppt
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptx
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
 
Input and Output.pptx
Input and Output.pptxInput and Output.pptx
Input and Output.pptx
 
ch11.ppt
ch11.pptch11.ppt
ch11.ppt
 
11.ppt
11.ppt11.ppt
11.ppt
 
Java-operators.ppt
Java-operators.pptJava-operators.ppt
Java-operators.ppt
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
 
IntroToOOP.ppt
IntroToOOP.pptIntroToOOP.ppt
IntroToOOP.ppt
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Recently uploaded (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 

JDBC.ppt database connectivity in java ppt

  • 1. Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt http://www.cs.gmu.edu/~offutt/ SWE 432 Design and Implementation of Software for the Web
  • 2. 3/19/2024 © Wu, Lee & Offutt 2 JDBC • JDBC (Java Database Connectivity) API allows Java programs to connect to databases • Database access is the same for all database vendors • The JVM uses a JDBC driver to translate generalized JDBC calls into vendor specific database calls • There are four general types of JDBC drivers – We will look at Type 4 …
  • 3. 3/19/2024 © Wu, Lee & Offutt 3 Pure Java Driver (Type 4) • These drivers convert the JDBC API calls to direct network calls using vendor-specific networking protocols by making direct socket connections with the database • It is the most efficient method to access database, both in performance and development time • It is the simplest to deploy • All major database vendors provide pure Java JDBC drivers for their databases and they are also available from third party vendors • For a list of JDBC drivers, refer to – http://industry.java.sun.com/products/jdbc/drivers
  • 4. 3/19/2024 © Wu, Lee & Offutt 4 Java Application DB Client Pure Java Driver (2) JDBC API JDBC Driver Data Source Server
  • 5. 3/19/2024 © Wu, Lee & Offutt 5 Typical JDBC Programming Procedure 1. Load the database driver 2. Obtain a connection 3. Create and execute statements (SQL queries) 4. Use result sets (tables) to navigate through the results 5. Close the connection
  • 6. 3/19/2024 © Wu, Lee & Offutt 6 Driver Manager • The purpose of the java.sql.DriverManger class in JDBC is to provide a common access layer on top of different database drivers used in an application • DriverManager requires that each driver required by the application must be registered before use, so that the DriverManager is aware of it • Load the database driver using ClassLoader : – Class.forName (“oracle.jdbc.driver.OracleDriver”);
  • 7. 3/19/2024 © Wu, Lee & Offutt 7 Connecting to a Database • Type 4 JDBC Driver – Oracle Server Class.forName (“oracle.jdbc.driver.OracleDriver”); con = DriverManager.getConnection ( “jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite”, “accountname", “password”); • Type 4 JDBC Driver – MySQL Server Class.forName (“org.gjt.mm.mysql.Driver”); con = DriverManager.getConnection (“jdbc:mysql://localhost/databasename”, uid, passwd);
  • 8. 3/19/2024 © Wu, Lee & Offutt 8 Creating Tables • Creating a Coffee table CREATE TABLE COFFEES (COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, SALES INTEGER, TOTAL INTEGER) • Creating JDBC statements Statement stmt = con.createStatement (); • Execute a statement stmt.executeUpdate (“CREATE TABLE COFFEES “ + “(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, “ + “SALES INTEGER, TOTAL INTEGER)”); SQL query
  • 9. 3/19/2024 © Wu, Lee & Offutt 9 Execute Statements • This uses executeUpdate because the SQL statement contained in createTableCoffees is a DDL (data definition language) statement • Statements that create a table, alter a table, or drop a table are all examples of DDL statements and are executed with the method executeUpdate • executeUpdate is also used to execute SQL statements that update a table
  • 10. 3/19/2024 © Wu, Lee & Offutt 10 Execute Statements • In practice, executeUpdate is used far more often to update tables than it is to create them because a table is created once but may be updated many times • The method used most often for executing SQL statements is executeQuery • executeQuery is used to execute SELECT statements, which comprise the vast majority of SQL statements
  • 11. 3/19/2024 © Wu, Lee & Offutt 11 Entering Data into a Table Statement stmt = con.createStatement(); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)"); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('French_Roast', 49, 8.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Espresso', 150, 9.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)" ); stmt.executeUpdate ( "INSERT INTO COFFEES " + "VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)" );
  • 12. 3/19/2024 © Wu, Lee & Offutt 12 Getting Data From a Table ResultSet rs = stmt.executeQuery ("SELECT COF_NAME, PRICE FROM COFFEES"); while (rs.next()) { String s = rs.getString ("COF_NAME"); float n = rs.getFloat ("PRICE"); System.out.println (s + " " + n); }
  • 13. 3/19/2024 © Wu, Lee & Offutt 13 JNDI Connection Manager JDBC Data Source Architecture Application JDBC Database
  • 14. 3/19/2024 © Wu, Lee & Offutt 14 Sample code • Sun JDBC tutorial