java in the database - apache db project · derby overview lightweight, 0-admin, pure java database...

27
Java in the Database Rick Hillegas Apache Derby September 12, 2006

Upload: duongdien

Post on 15-Feb-2019

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Java in the Database

Rick HillegasApache DerbySeptember 12, 2006

Page 2: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Plug It!

● Dock a free database in your code● Dock your code to the database

Page 3: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Sample Function public static int computeAge ( java.sql.Date date) { long interval = System.currentTimeMillis() - date.getTime(); return (int) (interval / MILLISECONDS_IN_YEAR); }

Page 4: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

General Overview

● Derby Overview● User Code in the Database● Demo

Page 5: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Derby Overview

● Lightweight, 0-admin, pure Java database● Follows ANSI-SQL and JDBC standards● Usage: embedded or client/server

Page 6: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

EmbeddedJava Virtual Machine

Derby

SpreadsheetApplication

Embedded JDBC Driver

Page 7: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Client/Server

Derby

DRDA

NetworkDriver

NetworkDriver

Network Server

BugTrackingApp Server

BugTrackingApp Server

Page 8: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

User Code

● Functions– Compute scalar result– Plug into query

● Procedures– Perform business operations– Plug into trigger or call from application

Page 9: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Function Placementselect computeAge( birthday ), lastName

from Student

where computeAge( birthday ) > 5

order by computeAge( birthday ), lastName

select birthday, count(*)

from Student

group by birthday

having computeAge( birthday ) > 10

Page 10: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Procedure Placement

call ScoreTestTaking( takingID )

create trigger ScoreTestWhenDone

after update of takingDate

on TestTaking

referencing new as testTakingRow

for each row mode db2sql

call ScoreTestTaking( testTakingRow.takingID )

Page 11: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Triggered ProcedureUpdate table (client side)

Fires trigger (server-side)

Calls procedure (server-side)

Updates table again (server-side)Loops through many rows (server-side)

Page 12: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Why?

● Integrity● Performance● Integration

Page 13: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Integrity

● Push business logic close to data● Enforce business rules in one place

Page 14: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Integrity Example

create table QuestionTaking

(

questionID int not null references Question( questionID ),

takingID int not null references TestTaking( takingID ),

actualChoice int not null,

unique( questionID, takingID ),

check ( vetChoice( actualChoice, questionID ) > 0 )

)

Page 15: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Performance

● Filter out noise early on● Reduce query execution time● Reduce network traffic

Page 16: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Query Tree

Page 17: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Filtered Tree

Page 18: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Integration

● Re-use existing freeware libraries● Join with external data

Page 19: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

ANSI SQL

● User-written functions● User-written procedures

Page 20: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Why Java?

● Expressive● Good exception handling

Page 21: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Why Java?

● Portable datatypes

Page 22: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Why Java?

● Large body of available freeware● Off-the shelf tool support, including

debuggers

Page 23: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Java Everywhere

● Same code can run in client, middle tier, and server

Client

Application Server

Database

ClientClient

Page 24: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Supporting Databases

● Derby● Postgres● Oracle● DB2● Sybase

Page 25: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Jars in Database

Database:1) Application Jar2) Jakarta math Jar

Client

Page 26: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Derby References

● Developer's Guide– “Loading classes from a database”– “Derby server-side programming”

Page 27: Java in the Database - Apache DB Project · Derby Overview Lightweight, 0-admin, pure Java database Follows ANSI-SQL and JDBC standards Usage: embedded or client/server

Demo Concepts

● Educational testing application● Students, Schools, Tests, Questions, Takings