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

Post on 22-Dec-2015

214 views 0 download

Transcript of 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 

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.

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

4

MysqlDBMS

OracleDBMS

Mysql Driver Oracle Driver

Driver Manager

JDBC API

Java Application

5

The Java Process Connects to the DBMS Server

Java ApplicationProcess

Database Server

6

Can also connect to a Web Server

Java ApplicationProcess

Database ServerWeb Server

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

8

SQL Query: Use

9

SQL Query: Show

10

SQL Query: Describe

11

SQL Query: Select

12

SQL Query: Describe

13

SQL Query: Select

14

SQL Query: Count(*)

15

16

SQL Query: Create and Insert

17

SQL Query: Load

(Input text file is tab-delimited)

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”;

19

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

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

22

23

24

25

26

27

28

Assignment

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

• Use supplied code; go from there.