Dbms 2021
Dbms 2021
Database
Dr.Sarwo M.Kom
STMIK MERCUSUAR
1
Java Database
1. Pengantar Database
2. Pengantar SQL
3. Koneksi Aplikasi Java ke Database
4. Studi Kasus Aplikasi Java Database
2
6.1 Pengantar Database
3
Introduction to Database
Database system is a computer based record
keeping system
It is a system whose overall purpose is to
record and maintain information that is
deemed important to the organization
Database is collection of stored operational
data which can be used and shared by
different applications and users of any
organization
4
Why Database
Database system provides the organization with
centralized control of its operational data,
which is one of its most valuable assets
This is totally opposite of the situation that is
happening in many organizations, where
typically each application has its own private
files (flat file). This makes the operational data
widely dispersed and difficult to control
5
Advantage of Centralized Database
Redundancy can be reduced
Inconsistency can be avoided
Data can be shared
Standards can be enforced
Security restrictions can be applied
Integrity can be maintained
Conflicting requirements can be balanced
6
Disadvantage of Database Systems
Database is more vulnerable to destruction thru:
• machine malfunction
• personal error
• Deliberate human tampering
Cost: the cost of required hardware, DB
development, and DB maintenance is high
Complexity: Due to its complexity, the user
should understand it well enough to use it
efficiently and effectively
7
Database Models - Product - Vendor
MODEL PRODUCT VENDOR
1. Relational DB2 IBMSQL/DS
Ingress Relational Tech.
Oracle Oracle corp
Access Microsoft
PostgreSQL
MySQL
2. Network DMS100 Unysis
IDMS Cullinet
3. Heirarchical IMS IBM
System 2000 Intel
4. Object oriented Starburst IBM
Gemstone
Orion8
Relational Database
Relational database is a collection of tables
Formally a table is called a relation
Database is a structure that can hold
information about tables, rows, and columns
9
Relational Database
Relational Relational Traditional
Model DBMS File System
10
Relational Database
1. Primary Key (PK): An attribute which can
uniquely identify each record (tuple) of a
relation (table)
11
Example of a Relational Database
Relation Name
Attribute
Primary Key (PK)
Sale
SalesNO Name Rate City Dept#
10 James 10 Dallas A211
12 Black 15 Denver F654
48 Black 8 WashDC A211
Tuple (record)
12
Example of a Relational Database
Customer
CustID Name Balance City SaleNo
132 Black 2000.00 Dallas 10
135 Tom 129.89 Denver 12
198 Tom (132.90) Dallas 10
13
Example of a Relational Database
Customer
CustID Name Balance City SaleNo
132 Black 2000.00 Dallas 10
135 Tom 129.89 Denver 12
198 Tom (132.90) Dallas 10
14
Example: Order Entry Database
Order OrderLine
ONO DATE CustID SalesNO ONO Oline# Part# Qty Part#
102 11/2/94 132 10 102 1 12.00 10 EX454
199 2/15/95 135 12 102 2 129.89 1 DE012
92 10/4/94 102 53 199 1 32.90 3 DC810
Customer
CustID Name Balance City SaleNo
132 Black 2000.00 Dallas 10
135 Tom 129.89 Denver 12
198 Tom (132.90) Dallas 10
Sales
SalesNO Name Rate City Dept#
10 James 10 Dallas A211
12 Black 15 Denver F654
48 Black 8 WashDC A211
15
Functionality of a DBMS
The programmer sees SQL, which has two
components:
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
16
How the Programmer Sees the DBMS
1. Start with DDL to create tables:
17
Transactions
Enroll “Mary Johnson” in “CSE444”:
BEGIN TRANSACTION;
IF everything-went-OK
THEN COMMIT;
ELSE ROLLBACK
19
Queries
Find all courses that “Mary” takes
SELECT C.name
FROM Students S, Takes T, Courses C
WHERE S.name=“Mary” and
S.ssn = T.ssn and T.cid = C.cid
20
Queries, Behind the Scene
sname
SELECT C.name
FROM Students S, Takes T, Courses C
WHERE S.name=“Mary” and
S.ssn = T.ssn and T.cid = C.cid cid=cid
sid=sid
name=“Mary”
22
SQL Introduction
Standard language for querying and manipulating
data
SQL = Structured Query Language
Many standards out there:
• ANSI SQL
• SQL92 (a.k.a. SQL2)
• SQL99 (a.k.a. SQL3)
• Vendors support various subsets of these
• What we discuss is common to all of them
23
SQL
Data Definition Language (DDL)
• Create/alter/delete tables and their attributes
Data Manipulation Language (DML)
• Query one or more tables
• Insert/delete/modify tuples in tables
Transact-SQL
• Idea: package a sequence of SQL statements server
24
Data Types in SQL
Characters:
• CHAR(20) -- fixed length
• VARCHAR(40) -- variable length
Numbers:
• BIGINT, INT, SMALLINT, TINYINT
• REAL, FLOAT -- differ in precision
• MONEY
Times and dates:
• DATE
• DATETIME -- SQL Server
Others... All are simple
25
SQL Data Type vs Java Data Type
SQL Data Type Java Data Type
INTEGER or INT int
REAL float
DOUBLE double
CHARACTER(n) or
Fixed-length String of length n
CHAR(n)
26
Tables in SQL
Table name Attribute names
Product
PName Price Category Manufacturer
Tuples or rows
27
Tables Explained
A tuple = a record
• Restriction: all attributes are of atomic type
A table = a set of tuples
• Like a list…
• …but it is unorderd: no first(), no next(), no last().
No nested tables, only flat tables are allowed!
28
Tables Explained
The schema of a table is the table name and its
attributes:
Product(PName, Price, Category, Manfacturer)
29
SQL Query
SELECT attributes
FROM relations (possibly multiple, joined)
WHERE conditions (selections)
30
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
FROM Product
WHERE category=‘Gadgets’
31
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
32
A Notation for SQL Queries
Input Schema
Output Schema
33
Selections
What goes in the WHERE clause:
x = y, x < y, x <= y, etc
• For number, they have the usual meanings
• For CHAR and VARCHAR: lexicographic ordering
Expected conversion between CHAR and VARCHAR
• For dates and times, what you expect...
Pattern matching on strings: s LIKE p
34
The LIKE operator
s LIKE p: pattern matching on strings
p may contain two special symbols:
• % = any sequence of characters
• _ = any single character
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
35
Eliminating Duplicates
Category
Gadgets
SELECT category
FROM Product Gadgets
Photography
Household
Compare to:
Category
SELECT DISTINCT category
FROM Product Gadgets
Photography
Household
36
Ordering the Results
Ties are broken by the second attribute on the ORDER BY list, etc.
37
Ordering the Results
SELECT Category
FROM Product
ORDER BY PName
?
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
38
Ordering the Results
Category
SELECT DISTINCT category Gadgets
FROM Product
ORDER BY category Household
Photography
Compare to:
?
SELECT DISTINCT category
FROM Product
ORDER BY PName
39
Joins in SQL
Connect two or more tables:
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
GizmoWorks 25 USA
What is
the Connection Canon 65 Japan
between
them ? Hitachi 15 Japan
40
Joins
Join
between Product
and Company
41
Joins in SQL
Product Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
PName Price
SingleTouch $149.99
42
Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)
Find all countries that manufacture some product in the ‘Gadgets’ category.
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
43
Joins in SQL
Product Company
PName Price Category Manufacturer
Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
44
Joins
Product (pname, price, category, manufacturer)
Purchase (buyer, seller, store, product)
Person(persname, phoneNumber, city)
45
Disambiguating Attributes
• Sometimes two relations have the same attribute:
Person(pname, address, worksfor)
Company(cname, address)
Which
SELECT DISTINCT pname, address address ?
FROM Person, Company
WHERE worksfor = cname
46
Tuple Variables
Product (pname, price, category, manufacturer)
Purchase (buyer, seller, store, product)
Person(persname, phoneNumber, city)
Find all stores that sold at least one product that the store
‘BestBuy’ also sold:
Answer (store)
47
Tuple Variables
General rule:
tuple variables introduced automatically by the system:
SELECT name
FROM Product
WHERE price > 100
Becomes:
SELECT Product.name
FROM Product AS Product
WHERE Product.price > 100
48
Renaming Columns
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
prodName askPrice
49
Studi Kasus Aplikasi Database
50
Aplikasi Database
1. Aplikasi Telepon
2. Aplikasi Guru
3. Aplikasi Bank
4. Aplikasi Penjualan Barang
51
Aplikasi Telepon
52
Aplikasi Telepon
1. Ekstrak xampplite dan jalankan xampp_start.exe
untuk mengaktifkan Apache dan MySQL
2. Buka browser, arahkan url ke http://localhost dan
klik link ke phpMyAdmin
3. Buat database telepon
4. Buat satu table bukutelepon, yang berisi field
dengan id sebagai primary key (PK):
1. id integer (auto increment)
2. nama varchar(20)
3. alamat varchar(50)
4. telepon varchar(20)
5. handphone varchar(20)
53
54
55
56
57
59
60
61
62
Aplikasi Telepon
1. Extract dan copy folder 05 JAVA DATABASE di
NetbeansProject anda
2. Di Netbeans buka file tersebut melalui Open
project
3. Cek package db.mysql (versi text) dan
dbgui.mysql (versi gui)
4. Program yang ada di kedua package tersebut
akan mengakses dan melakukan query ke
database telepon (table bukutelepon)
65
Cek Koneksi ke Database MySQL
String user="root"; String pswd ="";
String host="localhost"; String db="telepon"; String url="";
try {
Class.forName("com.mysql.jdbc.Driver");
url="jdbc:mysql://"+ host +"/"+ db +"?user=" + user +
"&password="+ pswd;
Connection conn=DriverManager.getConnection(urlValue);
System.out.println("koneksi sukses");
conn.close();
} catch (SQLException e){
System.out.println("koneksi gagal " + e.toString());
} catch(ClassNotFoundException e) {
System.out.println("jdbc.Driver tidak ditemukan");
66
Cek Koneksi ke Database PostgreSQL
String user="root"; String pswd ="";
String host="localhost"; String db="telepon"; String url="";
try {
Class.forName(“org.postgresql.Driver");
url="jdbc:postgresql://"+ host +"/"+ db +"?user=" + user +
"&password="+ pswd;
Connection conn=DriverManager.getConnection(urlValue);
System.out.println("koneksi sukses");
conn.close();
} catch (SQLException e){
System.out.println("koneksi gagal " + e.toString());
} catch(ClassNotFoundException e) {
System.out.println("jdbc.Driver tidak ditemukan");
67
Aplikasi Guru
68
Aplikasi Guru
1. Buat database Guru
2. Buat satu table dataguru, yang berisi field dengan
nip sebagai primary key (PK). Field yang lain adalah
seperti di bawah:
1. nip integer (auto increment)
2. nama varchar(30)
3. status varchar(20)
4. institusi varchar(30)
5. kota varchar(30)
6. handphone varchar(20)
7. jeniskelamin varchar(20)
8. bidangstudi varchar(30)
69
Tugas: Aplikasi Guru
3. Pahami program yang ada di package
db.mysql
4. Buat 5 class java yang melakukan query ke
database Guru:
1. GuruConnection.java
2. GuruInsert.java
3. GuruRead.java
4. GuruUpdate.java
5. GuruDelete.java
70
Tugas: Aplikasi Guru
3. Pahami program yang ada di package
dbgui.mysql
4. Buat 1 class MenuUtama dan 4 class java GUI
yang melakukan query ke database Guru:
1. GuruInsertUI.java
2. GuruReadUI.java
3. GuruUpdateUI.java
4. GuruDeleteUI.java
5. MenuUtama.java
71
Aplikasi Bank
72
Aplikasi Bank
1. Pahami dengan baik Case Study: A Bank
Database yang terdapat pada buku
Hortsmann (halaman 871)
2. Buat dua tabel database: BankCustomer dan
Account
3. Buat dua class yang mendefinisikan dan
mengoperasikan aplikasi Bank: Bank.java dan
BankAccount.java
4. Buat satu class yang berisi method main yang
mengeksekusi aplikasi bank
73
Aplikasi Penjualan Barang
74
Aplikasi Penjualan Barang (Quantum)
1. Ekstrak quantum.zip
2. Buat database sib di MySQL dan import sib.sql
3. Open project quantum
4. Lakukan pengecekan dan perbaikan error yang
ada (klik kanan di project dan pilih Resolve
Reference Problem)
5. Build dan jalankan program
6. Pelajari dengan baik source codenya
75
Tugas
Kembangkan aplikasi java berbasis GUI yang mengakses database
MySQL. Fitur utama dari aplikasi adalah kemampuan untuk CRUD
(create, read (listing), update, delete) data dari database MySQL dan
fitur transaksi serta reporting. Gunakan lebih dari satu table
Pilih aplikasi dari list di bawah (digit terakhir NIM):
1. Aplikasi Online Penjualan Buku 6. Aplikasi Sirkulasi Perpustakaan
2. Aplikasi Online Penjualan Handphone 7. Aplikasi Rental Mobil
3. Aplikasi Online Pengelolaan KRS 8. Aplikasi Penjualan Handphone
4. Aplikasi Online Penjualan Tiket Pesawat 9. Aplikasi Penjualan CD Musik
5. Aplikasi Online Penjualan Tiket Kereta 0. Aplikasi Sewa PC