Database JDBC Overview - University of Virginia School of...
Transcript of Database JDBC Overview - University of Virginia School of...
![Page 1: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/1.jpg)
DatabaseJDBC Overview
CS 4640 Programming Languages
for Web Applications
Fall 2017 - CS 4640 1©Praphamontripong
![Page 2: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/2.jpg)
JDBC• JDBC 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
• JDBC != Java Database Connectivity• http://java.sun.com/docs/books/jdbc/intro.html• Excerpt :
• “JDBC (TM) is a Java (TM) API for executing SQL statements. (As a point of interest, JDBC is a trademarked name and is not an acronym; nevertheless, JDBC is often thought of as standing for `Java Database Connectivity’.) ”
2Fall 2017 - CS 4640 ©Praphamontripong
![Page 3: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/3.jpg)
Pure Java Driver (Type 4)
3
• There are four general types of JDBC drivers• We will look at Type 4
JavaApplication
DB Client
JDBCAPI
JDBC Driver
Data Source
Server
Type 4 drivers are• Efficient• Simple to use and
deploy• Most commonly used
Fall 2017 - CS 4640 ©Praphamontripong
![Page 4: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/4.jpg)
Typical JDBC Programming Process1. Load the database driver2. Obtain a connection3. Create and execute statements
• executeQuery – to execute SQL select statements• executeUpdate – to execute SQL statements that update a table
4. Use result sets to navigate through the results5. Close the connection
4Fall 2017 - CS 4640 ©Praphamontripong
![Page 5: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/5.jpg)
Simple Example
5Fall 2017 - CS 4640 ©Praphamontripong
![Page 6: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/6.jpg)
Simple Example (2)
6
Load and register the database driver using ClassLoader
Connect to a database
Create SQL statement object
Close SQL statement object and connection
Unregister the driver
SEVERE: The web application [/this-context] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
DB-related processes
Fall 2017 - CS 4640 ©Praphamontripong
![Page 7: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/7.jpg)
Simple Example (select)
7
Prepare a query
Execute a query and store all rows retrieved in a ResultSet object
Access a value of column “user_id”
Close a ResultSet object
More rows?
By default, a ResultSet object is read-only and has a cursor that moves forward only, i.e., next().
Fall 2017 - CS 4640 ©Praphamontripong
![Page 8: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/8.jpg)
Simple Example (create table)
8
Prepare a query
Execute a query to create a table
Successfully create a table?
Fall 2017 - CS 4640 ©Praphamontripong
![Page 9: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/9.jpg)
Simple Example (insert)
9
Prepare a query
Execute a query to insert information into a table
Successfully insert information?
Fall 2017 - CS 4640 ©Praphamontripong
![Page 10: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/10.jpg)
Simple Example (update)
10
Prepare a query
Execute a query to update information
Successfully update information?
By careful !! update without a “where” clause
will update all rows in the table
Fall 2017 - CS 4640 ©Praphamontripong
![Page 11: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/11.jpg)
Simple Example (delete)
11
Prepare a query
Execute a query to update information
Successfully update information?
By careful !! delete from student; ordelete * from student;
will delete all rows in the table
Fall 2017 - CS 4640 ©Praphamontripong
![Page 12: Database JDBC Overview - University of Virginia School of ...up3f/cs4750/slides/4640Lec20-JDBC-F17.pdf · JDBC is often thought of as standing for `Java Database Connectivity’.)](https://reader034.fdocuments.in/reader034/viewer/2022050511/5f9bc5bb8194604c76759f3d/html5/thumbnails/12.jpg)
JDBC Summary• Most large web apps use databases to make data persistent
• The techniques for accessing databases from Java programs are identical in web apps as in stand-alone Java programs
• Read further for information on how to set up, use a database, and how to construct SQL queries
12Fall 2017 - CS 4640 ©Praphamontripong