8. Java Databases and JDBC 1. Introduction to Databases

Содержание

Слайд 2

Relational DBMS A DBMS in which data is stored in tables

Relational DBMS

A DBMS in which data is stored in tables and

the relationships among the data are also stored in tables
The data can be accessed or reassembled in many different ways without having to change the table forms.

*

Infopulse Training Center

Слайд 3

Relational DBMS Commercial Oracle MS SQL Server DB2 Free Derby (Java

Relational DBMS

Commercial
Oracle
MS SQL Server
DB2

Free
Derby (Java DB)
MySQL

*

Infopulse Training Center

Слайд 4

Cash Management System * Infopulse Training Center

Cash Management System

*

Infopulse Training Center

Слайд 5

Merchant Info Name Bank Bank account Charge percent Aggregation period Minimal sum * Infopulse Training Center

Merchant Info

Name
Bank
Bank account
Charge percent
Aggregation period
Minimal sum

*

Infopulse Training Center

Слайд 6

Customer Info Name Address Email Credit card No Credit card type

Customer Info

Name
Address
Email
Credit card No
Credit card type
Credit card maturity date

*

Infopulse Training Center

Слайд 7

Payment info Date Customer Merchant Goods description Sum * Infopulse Training Center

Payment info

Date
Customer
Merchant
Goods description
Sum

*

Infopulse Training Center

Слайд 8

Java DB Java DB is Oracle's supported distribution of the Apache

Java DB

Java DB is Oracle's supported distribution of the Apache Derby

open source database
It supports standard ANSI/ISO SQL through the JDBC and Java EE APIs
Java DB is included in the JDK
http://www.oracle.com/technetwork/java/javadb/overview/index.html

*

Infopulse Training Center

Слайд 9

Eclipse & Java DB Creating a Driver Definition for Apache Derby

Eclipse & Java DB

Creating a Driver Definition for Apache Derby
Creating an

Apache Derby Connection Profile
Connecting to Apache Derby
Creating and Executing a SQL Query

*

Infopulse Training Center

Слайд 10

Driver Definition (1 of 2) Start Eclipse Menu Window -> Preferences

Driver Definition (1 of 2)

Start Eclipse
Menu Window -> Preferences
Expand Data Management

-> Connectivity -> Driver Definitions
Click Add button
Select “Derby Embedded JDBC Driver” in Name/Type tab

*

Infopulse Training Center

Слайд 11

Driver Definition (2 of 2) Select derby.jar in Jar list tab

Driver Definition (2 of 2)

Select derby.jar in Jar list tab and

click Add JAR/Zip button
Select full path to derby.jar (usually C:\Program Files\Java\jdk1.7.0_05\db\lib)
Click Open button
Click Ok button

*

Infopulse Training Center

Слайд 12

Eclipse & Java DB Creating a Driver Definition for Apache Derby

Eclipse & Java DB

Creating a Driver Definition for Apache Derby
Creating an

Apache Derby Connection Profile
Connecting to Apache Derby
Creating and Executing a SQL Query

*

Infopulse Training Center

Слайд 13

Connection Profile Switch to the Database Development perspective In Data Source

Connection Profile

Switch to the Database Development perspective
In Data Source Explorer, right-click

Database Connections and select New
Select Derby, change Name of profile (optionally) and click Next
Select Database location and click Finish

*

Infopulse Training Center

Слайд 14

Eclipse & Java DB Creating a Driver Definition for Apache Derby

Eclipse & Java DB

Creating a Driver Definition for Apache Derby
Creating an

Apache Derby Connection Profile
Connecting to Apache Derby
Creating and Executing a SQL Query

*

Infopulse Training Center

Слайд 15

Connecting to the Database In the Database Development perspective, expand Database

Connecting to the Database

In the Database Development perspective, expand Database Connections

in the Data Source Explorer
Right-click the connection profile that you created and select Connect

*

Infopulse Training Center

Слайд 16

Eclipse & Java DB Creating a Driver Definition for Apache Derby

Eclipse & Java DB

Creating a Driver Definition for Apache Derby
Creating an

Apache Derby Connection Profile
Connecting to Apache Derby
Creating and Executing a SQL Query

*

Infopulse Training Center

Слайд 17

SQL Query In the Database Development perspective, expand Database Connections in

SQL Query

In the Database Development perspective, expand Database Connections in the

Data Source Explorer
Right-click the connection profile that you created and select “Open SQL Scrapbook”
Select database
Create SQL query in the editor field
Right-click in the editor and select Execute All.

*

Infopulse Training Center

Слайд 18

Merchant Info Name Bank Bank account Charge percent Aggregation period Minimal sum * Infopulse Training Center

Merchant Info

Name
Bank
Bank account
Charge percent
Aggregation period
Minimal sum

*

Infopulse Training Center

Слайд 19

Create Merchant Table CREATE TABLE merchant ( id INT NOT NULL

Create Merchant Table

CREATE TABLE merchant
(
id INT NOT NULL GENERATED ALWAYS

AS IDENTITY,
name VARCHAR(60) NOT NULL,
bankName VARCHAR (100) NOT NULL,
swift VARCHAR (40) NOT NULL,
account VARCHAR (20) NOT NULL,
charge DECIMAL(5,2) NOT NULL,
period SMALLINT NOT NULL,
minSum DECIMAL (19,2) NOT NULL,
total DECIMAL(19,2),
PRIMARY KEY (id)
);

*

Infopulse Training Center

Слайд 20

Fill Merchant Table INSERT INTO merchant (name, charge, period, minSum, bankName,

Fill Merchant Table

INSERT INTO merchant
(name, charge, period, minSum,
bankName,

swift, account)
VALUES('Jim Smith Ltd.', 5.1, 1, 100.0,
'Chase Manhatten', 'AA245BXW',
'247991002');

*

Infopulse Training Center

Слайд 21

Display Merchant Data * Infopulse Training Center select * from merchant;

Display Merchant Data

*

Infopulse Training Center

select * from merchant;

Слайд 22

Create Customer Table Customer Info Name Address Email Credit card No

Create Customer Table

Customer Info
Name
Address
Email
Credit card No
Credit card type
Credit card maturity date

*

Infopulse

Training Center
Слайд 23

Create Customer Table CREATE TABLE customer ( id INT NOT NULL

Create Customer Table

CREATE TABLE customer
(
id INT NOT NULL GENERATED ALWAYS

AS IDENTITY,
name VARCHAR(60) NOT NULL,
address VARCHAR(300) NOT NULL,
email VARCHAR(90) NOT NULL,
ccNo VARCHAR(20) NOT NULL,
ccType VARCHAR(60) NOT NULL,
maturity DATE,
PRIMARY KEY (id)
);

*

Infopulse Training Center

Слайд 24

Fill Customer Table INSERT INTO customer (name, address, email, ccNo, ccType,

Fill Customer Table

INSERT INTO customer
(name, address, email, ccNo, ccType, maturity)
values('Dan

Nelis',
'Vosselaar st. 19, Trnaut, Belgium', 'Dan@adw.com',
'11345694671214',
'MasterCard',
'2014-07-31');

*

Infopulse Training Center

Слайд 25

Display Customer Data select * from customer * Infopulse Training Center

Display Customer Data

select * from customer

*

Infopulse Training Center

Слайд 26

Create Payment Table Payment info Date Customer Merchant Goods description Sum * Infopulse Training Center

Create Payment Table

Payment info
Date
Customer
Merchant
Goods description
Sum

*

Infopulse Training Center

Слайд 27

Create Payment Table CREATE TABLE payment ( id INT NOT NULL

Create Payment Table

CREATE TABLE payment
(
id INT NOT NULL GENERATED ALWAYS

AS IDENTITY,
dt TIMESTAMP NOT NULL,
merchantId INT CONSTRAINT mer_fk references merchant,
customerId INT CONSTRAINT cust_fk references customer,
goods VARCHAR(500),
total DECIMAL(15,2),
charge DECIMAL(15,2),
PRIMARY KEY (id)
);

*

Infopulse Training Center

Слайд 28

Fill Payment Table insert into payment (dt, merchantId, customerId, goods, total)

Fill Payment Table

insert into payment
(dt, merchantId, customerId, goods, total)
values('2012-07-12 10:00:14', 3,

1,
'CD Europe Maps', 12.08);

*

Infopulse Training Center

Слайд 29

Display Payment Data Select * from payment * Infopulse Training Center

Display Payment Data

Select * from payment

*

Infopulse Training Center

Слайд 30

CM Database Schema * Infopulse Training Center

CM Database Schema

*

Infopulse Training Center

Слайд 31

Select Statement Don’t use * in select! * Infopulse Training Center

Select Statement

Don’t use * in select!

*

Infopulse Training Center

Слайд 32

Select Statement SELECT dt, merchantId, customerId, goods, total FROM payment WHERE

Select Statement

SELECT dt, merchantId, customerId, goods, total FROM payment WHERE merchantId

= 3;

*

Infopulse Training Center

Слайд 33

Select Statement SELECT dt, merchantId, customerId, goods, total FROM payment WHERE

Select Statement

SELECT dt, merchantId, customerId, goods, total FROM payment WHERE merchantId

= 3;

*

Infopulse Training Center

Слайд 34

Select Statement SELECT dt, merchantId, customerId, goods, total FROM payment ORDER

Select Statement

SELECT dt, merchantId, customerId, goods, total FROM payment ORDER BY

merchantId;

*

Infopulse Training Center

Слайд 35

Select Statement SELECT dt, merchantId, customerId, goods, total FROM payment ORDER

Select Statement

SELECT dt, merchantId, customerId, goods, total FROM payment ORDER BY

merchantId;

*

Infopulse Training Center

Слайд 36

Select Statement SELECT sum(total) FROM payment WHERE customerId = 2; * Infopulse Training Center

Select Statement

SELECT sum(total) FROM payment WHERE customerId = 2;

*

Infopulse Training Center

Слайд 37

Select Statement SELECT sum(total) FROM payment WHERE customerId = 2; Output

Select Statement

SELECT sum(total) FROM payment WHERE customerId = 2;
Output is 2134.45

*

Infopulse

Training Center
Слайд 38

Select Statement SELECT merchantId, count(*) as n, sum(total) as total FROM

Select Statement

SELECT merchantId, count(*) as n, sum(total) as total FROM payment

GROUP BY merchantId;

*

Infopulse Training Center

Слайд 39

Select Statement SELECT merchantId, count(*) as n, sum(total) as total FROM

Select Statement

SELECT merchantId, count(*) as n, sum(total) as total FROM payment

GROUP BY merchantId;

*

Infopulse Training Center

Слайд 40

Select Statement SELECT customerId, sum(total) FROM payment GROUP BY customerId HAVING count(*)>2; * Infopulse Training Center

Select Statement

SELECT customerId, sum(total) FROM payment GROUP BY customerId HAVING count(*)>2;

*

Infopulse

Training Center
Слайд 41

Select Statement SELECT customerId, sum(total) FROM payment GROUP BY customerId HAVING count(*)>2; * Infopulse Training Center

Select Statement

SELECT customerId, sum(total) FROM payment GROUP BY customerId HAVING count(*)>2;

*

Infopulse

Training Center
Слайд 42

Join Operations SELECT p.dt, m.name as merchant, c.name as customer, p.goods,

Join Operations

SELECT p.dt, m.name as merchant, c.name as customer,
p.goods,

p.total
FROM payment p
LEFT OUTER JOIN merchant m on m.id = p.merchantId
LEFT OUTER JOIN customer c on c.id = p.customerId;
SELECT p.dt, m.name as merchant, c.name as customer, p.goods, p.total
FROM payment p, merchant m, customer c
WHERE m.id = p.merchantId and c.id = p.customerId;

*

Infopulse Training Center

Слайд 43

Join Operations * Infopulse Training Center

Join Operations

*

Infopulse Training Center

Слайд 44

Update Payments * Infopulse Training Center

Update Payments

*

Infopulse Training Center

Слайд 45

Update Statement UPDATE payment SET charge = total * 0.034 WHERE

Update Statement

UPDATE payment SET charge = total * 0.034 WHERE id

= 1;

*

Infopulse Training Center

Слайд 46

Update Statement UPDATE payment SET charge = total * 0.034 WHERE

Update Statement

UPDATE payment SET charge = total * 0.034 WHERE id

= 1;

*

Infopulse Training Center

Слайд 47

Update Statement UPDATE payment SET charge = (SELECT p.total * m.charge

Update Statement

UPDATE payment
SET charge = (SELECT p.total * m.charge /

100.0
FROM payment p, merchant m
WHERE m.id = p.merchantId and p.id = 2)
WHERE id = 2;

*

Infopulse Training Center

Слайд 48

Update Statement * Infopulse Training Center

Update Statement

*

Infopulse Training Center

Слайд 49

Update Statement UPDATE payment p SET charge = total * (SELECT

Update Statement

UPDATE payment p SET charge = total * (SELECT charge

FROM merchant m WHERE m.id = p.merchantId) / 100.0

*

Infopulse Training Center

Слайд 50

Update Statement * Infopulse Training Center

Update Statement

*

Infopulse Training Center

Слайд 51

Update Merchants * Infopulse Training Center

Update Merchants

*

Infopulse Training Center

Слайд 52

Update Merchants UPDATE merchant m SET total = (SELECT sum(total -

Update Merchants

UPDATE merchant m SET total =
(SELECT sum(total - charge)


FROM payment p WHERE p.merchantId=m.id)

*

Infopulse Training Center

Слайд 53

Update Merchants * Infopulse Training Center

Update Merchants

*

Infopulse Training Center