1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

28
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

Page 1: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

1

Foundations of Software Design

Lecture 27: Java Database Programming Marti HearstFall 2002 

Page 2: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

2

Accessing DBMSs

• DBMS: DataBase Management System• SQL: standard query language

– Typed at command line– Forms-based interfaces– Direct-manipulation interfaces

• Programming APIs:– ODBC is an implementation API for SQL

• Open DataBase Connectivity• A platform-independent standard• JDBC is a java version of this

– Special scripting languages also exist• PHP, Cold Fusion, etc.

Page 3: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

3

Programming DBMSs

• The DBMS Server– A continuously running process– Supports many different databases simultaneously

• The data can be owned by different people• Each database within the DBMS has its own name,

owners, password, tables

– The application program connects to the DBMS server• First makes a connection• Then enters a loop of issuing queries and receiving

results• Somewhat like a web server in this respect

Page 4: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

4

MysqlDBMS

OracleDBMS

Mysql Driver Oracle Driver

Driver Manager

JDBC API

Java Application

Page 5: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

5

The Java Process Connects to the DBMS Server

Java ApplicationProcess

Database Server

Page 6: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

6

Can also connect to a Web Server

Java ApplicationProcess

Database ServerWeb Server

Page 7: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

7

Using SQL

• Issue a query to the database• Types of queries:

– Find out which tables are available– Add a table– Add data to a table– Update data in a table– Select rows from a table– Join rows across two or more tables– … many more

Page 8: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

8

SQL Query: Use

Page 9: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

9

SQL Query: Show

Page 10: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

10

SQL Query: Describe

Page 11: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

11

SQL Query: Select

Page 12: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

12

SQL Query: Describe

Page 13: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

13

SQL Query: Select

Page 14: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

14

SQL Query: Count(*)

Page 15: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

15

Page 16: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

16

SQL Query: Create and Insert

Page 17: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

17

SQL Query: Load

(Input text file is tab-delimited)

Page 18: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

18

Sample SQL Join

A join combines rows from different tables

select t1.item, t1.name from items as t1, item_nature as t2, nature as t3 where t1.item = t2.item and t2.id = t3.id and t3.name = “hare”;

Page 19: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

19

Page 20: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

20

Using the JDBC API

import java.sql.*;http://java.sun.com/j2se/1.4/docs/api/java/sql/package-summary.html

Facilities:– Making a connection with a database via the

DriverManager facility – Sending SQL statements to a database – Retrieving and updating the results of a query – Standard mappings for SQL types to classes and

interfaces in the Java programming language – Custom mapping an SQL user-defined type (UDT) to a

class in the Java programming language – Metadata – Exceptions

Page 21: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

21

Using SQL in JDBC

• Compose a Query – Represented as a String

• Create a Statement• Instantiate Statement with a Query• Execute the Query

– Send it to the DBMS

• Receive the ResultSet– Optionally extract the ResultSetMetaData

• Process the contents of the ResultSet• Repeat

Page 22: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

22

Page 23: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

23

Page 24: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

24

Page 25: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

25

Page 26: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

26

Page 27: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

27

Page 28: 1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.

28

Assignment

• Write java code to create and then use the contents of a DBMS database

• Use supplied code; go from there.