Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming in Java

Similar presentations


Presentation on theme: "Database Programming in Java"— Presentation transcript:

1 Database Programming in Java

2 What are you going to learn about today?
MySQL database programming Relational DB model How to write SQL queries

3 Database (DB): Organized collection of data
Database Management System (DBMS): Controls the creation, maintenance, and use of a DB

4 Why use a DBMS? Data independence: Applications need not be concerned with how data is stored or accessed Provides a lot of functionality that would be silly to implement yourself: Sharing (network) Customizable security Integrity

5 Two key aspects of a DBMS
Database model: How DB is structured and used Examples: Relational, Object-Oriented, Hierarchical Query language: Types of questions you can ask Examples: SQL, XQuery Relational + SQL is most common, so we’ll use MySQL

6 DB Tools DB client tools Code using DB library
Example: MySQL Workbench Code using DB library Lib example: Java Database Connectivity (JDBC)

7 Relational Model Concepts

8 Example Tables Authors AuthorISBN Publishers Titles AuthorID FirstName
LastName YearBorn 1 Ayn Rand 1905 2 Peter Benchley 1940 AuthorISBN Publishers PublisherID PublisherName 1 Bobbs Merrill 2 Random House ISBN AuthorID 1 2 Titles ISBN Title EditionNumber YearPublished Description PublisherID The Fountainhead 1 1943 Atlas Shrugged 1957 2 Jaws 1973

9 Primary versus Foreign Keys
Primary key: Uniquely identifies each record in table Foreign key: Field in table A such that the field is a primary key in one other table B Authors AuthorID FirstName LastName YearBorn 1 Ayn Rand 1905 AuthorISBN ISBN AuthorID 1

10 CRUD-to-SQL Mapping CRUD Operation SQL Statement Create INSERT
Read (Retrieve) SELECT Update (Modify) UPDATE Delete (Destroy) DELETE For complete documentation, see:

11 Example SELECT Queries—Let’s try them!
SELECT * FROM Authors SELECT AuthorID, LastName FROM Authors SELECT * FROM Authors WHERE YearBorn > 1910 SELECT * FROM Authors WHERE LastName LIKE ‘r%’ SELECT * FROM Authors WHERE LastName LIKE ‘_e%’ SELECT * FROM Authors WHERE LastName REGEX ‘[a-r]*’ SELECT * FROM Authors WHERE LastName REGEX ‘[a-r]*’ ORDER BY LastName ASC For complete documentation, see

12 Use JOIN to merge data from multiple tables
SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID ORDER BY LastName, FirstName SELECT Titles.Title, Authors.LastName, Publishers.PublisherName FROM (Publishers INNER JOIN Titles ON Publishers.PublisherID = Titles.PublisherID) INNER JOIN (Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID) ON Titles.ISBN = AuthorISBN.ISBN For complete documentation, see

13 Basic steps for performing query with JDBC
Load driver class (only needs to be done once) Connect to DB Create/initialize statement Execute statement (returns ResultSet) Process ResultSet Close connection (closes statement and ResultsSet) For more complete documentation, see:

14 Let’s take a tour of a web app that uses JDBC
Let’s take a tour of a web app that uses JDBC

15 What could go wrong with this code? (Hint: Recall last lecture)
Check Use // Add 1 to each player’s Score Statement qst = con.createStatement(); rs = qst.executeQuery("SELECT * FROM Players"); while (rs.next()) { int playerID = rs.getInt("PlayerID"); int score = rs.getInt("Score"); Statement ust = con.createStatement(); ust.executeUpdate("UPDATE Players SET PlayerID = '" + (score+1) + "' WHERE PlayerID = '" + playerID + "'"); } Score could change TOCTOU race condition! To avoid races, see transactions and/or table locks:

16 Summary Relational DBs CRUD to SQL mapping JDBC programming
Summary Relational DBs CRUD to SQL mapping JOIN operation JDBC programming Watch out for race conditions!


Download ppt "Database Programming in Java"

Similar presentations


Ads by Google