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
2/8/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 …
2/8/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
2/8/2024 © Wu, Lee & Offutt 4
Java
Application
DB Client
Pure Java Driver (2)
JDBC
API
JDBC Driver
Data Source
Server
2/8/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
2/8/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”);
2/8/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);
2/8/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
2/8/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
2/8/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
2/8/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)" );
2/8/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);
}
2/8/2024 © Wu, Lee & Offutt 13
JNDI
Connection Manager
JDBC Data Source Architecture
Application JDBC Database
2/8/2024 © Wu, Lee & Offutt 14
Sample code
• Sun JDBC tutorial

More Related Content

Similar to java database connectivity drivers tutorial

Similar to java database connectivity drivers tutorial (20)

jdbc
jdbcjdbc
jdbc
 
JDBC
JDBCJDBC
JDBC
 
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
 
Weblogic 101 for dba
Weblogic  101 for dbaWeblogic  101 for dba
Weblogic 101 for dba
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
JavaFX Enterprise (JavaOne 2014)
JavaFX Enterprise (JavaOne 2014)JavaFX Enterprise (JavaOne 2014)
JavaFX Enterprise (JavaOne 2014)
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE Introduction
 
Jdbc
JdbcJdbc
Jdbc
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc 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
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Chapter2 j2ee
Chapter2 j2eeChapter2 j2ee
Chapter2 j2ee
 
Jdbc
JdbcJdbc
Jdbc
 

Recently uploaded

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 

Recently uploaded (20)

Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 

java database connectivity drivers tutorial

  • 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. 2/8/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. 2/8/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. 2/8/2024 © Wu, Lee & Offutt 4 Java Application DB Client Pure Java Driver (2) JDBC API JDBC Driver Data Source Server
  • 5. 2/8/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. 2/8/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. 2/8/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. 2/8/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. 2/8/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. 2/8/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. 2/8/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. 2/8/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. 2/8/2024 © Wu, Lee & Offutt 13 JNDI Connection Manager JDBC Data Source Architecture Application JDBC Database
  • 14. 2/8/2024 © Wu, Lee & Offutt 14 Sample code • Sun JDBC tutorial