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

PPT
JDBC.ppt
PPT
java database connectivity drivers tutorial
PPTX
JDBC ppt
PDF
Unit 5.pdf
PPT
Chap3 3 12
PPTX
java 4 Part 1 computer science.pptx
PPTX
1. java database connectivity (jdbc)
JDBC.ppt
java database connectivity drivers tutorial
JDBC ppt
Unit 5.pdf
Chap3 3 12
java 4 Part 1 computer science.pptx
1. java database connectivity (jdbc)

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

PDF
10 J D B C
PPT
JDBC.ppt
PPT
PPTX
Jdbc in servlets
PDF
JavaFX Enterprise (JavaOne 2014)
PPT
Basic Java Database Connectivity(JDBC)
PPT
Jdbc connectivity
PPT
4-INTERDUCATION TO JDBC-2019.ppt
PPTX
java.pptx
PPT
Java database connectivity
PPT
Java database connectivity
PDF
Introduction to Java Database Connectivity (JDBC)
PDF
PPT
PPTX
Java Database Connectivity (JDBC)
PPTX
AJppt.pptx
PDF
4_59788783hhhhhhhhhhhhhhhhhhhhhhhhhhhhh34715564451.pdf
PPTX
Jdbc new
10 J D B C
JDBC.ppt
Jdbc in servlets
JavaFX Enterprise (JavaOne 2014)
Basic Java Database Connectivity(JDBC)
Jdbc connectivity
4-INTERDUCATION TO JDBC-2019.ppt
java.pptx
Java database connectivity
Java database connectivity
Introduction to Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
AJppt.pptx
4_59788783hhhhhhhhhhhhhhhhhhhhhhhhhhhhh34715564451.pdf
Jdbc new
Ad

More from kavitamittal18 (20)

PPT
awt.ppt java windows programming lecture
PPT
ejb.ppt java lecture notes enterprise java
PPT
pptTopic2IntroductionToJavaProgramming.ppt
PPT
11MappingDesigntoCode.ppt Software engineering
PPT
UseCase.ppt software engineering use3 cases
PPT
11MappingDesigntoCode.ppt ooad software software
PPT
Introduction.ppt wireless
PPT
CellularNetworks.ppt ppt
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PPT
CSL101_Ch1.ppt Computer Science
PPT
maincse-150510153437-lva1-app68Computer Science92.ppt
PPT
Programming language basics.ppt Computer Science
PPT
02-chapter-1.ppt programming languages 10
PPT
CS553_ST7_Ch14-CellularWirelessNetworks.ppt
PPT
Lec7!JavaThreads.ppt java multithreading
PPT
chapter7.ppt java programming lecture notes
PPT
09slide.ppt oops classes and objects concept
PPT
480 GPS Tech mobile computing presentation
PPT
gsm-archtecture.ppt mobile computing ppt
PPT
AdHocTutorial.ppt
awt.ppt java windows programming lecture
ejb.ppt java lecture notes enterprise java
pptTopic2IntroductionToJavaProgramming.ppt
11MappingDesigntoCode.ppt Software engineering
UseCase.ppt software engineering use3 cases
11MappingDesigntoCode.ppt ooad software software
Introduction.ppt wireless
CellularNetworks.ppt ppt
Dr.C S Prasanth-Physics ppt.pptx computer
CSL101_Ch1.ppt Computer Science
maincse-150510153437-lva1-app68Computer Science92.ppt
Programming language basics.ppt Computer Science
02-chapter-1.ppt programming languages 10
CS553_ST7_Ch14-CellularWirelessNetworks.ppt
Lec7!JavaThreads.ppt java multithreading
chapter7.ppt java programming lecture notes
09slide.ppt oops classes and objects concept
480 GPS Tech mobile computing presentation
gsm-archtecture.ppt mobile computing ppt
AdHocTutorial.ppt
Ad

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Construction Project Organization Group 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
composite construction of structures.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Current and future trends in Computer Vision.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Digital Logic Computer Design lecture notes
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Well-logging-methods_new................
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
PPT on Performance Review to get promotions
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Artificial Intelligence
Foundation to blockchain - A guide to Blockchain Tech
Construction Project Organization Group 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
composite construction of structures.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Geodesy 1.pptx...............................................
Current and future trends in Computer Vision.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)
Digital Logic Computer Design lecture notes
Embodied AI: Ushering in the Next Era of Intelligent Systems
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Well-logging-methods_new................
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT on Performance Review to get promotions
UNIT-1 - COAL BASED THERMAL POWER PLANTS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Artificial Intelligence

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