187
Prerequisite knowledge RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. SQL, SQL - DML: Predicate based queries, joins. Transactions, ACID principle: Isolation level 1 - 4.

Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

  • Upload
    others

  • View
    38

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Prerequisite knowledge• RDBMS schema and SQL - DDL:

PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes.

• SQL, SQL - DML:

Predicate based queries, joins.

• Transactions, ACID principle:

Isolation level 1 - 4.

Page 2: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persistence [Bauer2015]Persistence allows an object to outlive the process that created it. The state of the object may be stored to disk and an object with the same state re-created at some point in the future.

Page 3: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Java™ transient instancespublic class User { String commonName; // Common name e.g. 'Joe Bix' String uid; // Unique login name e.g. 'bix' ...// getters, setters and other stuff

}//------------------------------------// Thread lifespan (transient instance)User u = new User("Joe Bix", "bix");

Page 4: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

RDBMS persistent recordsCREATE TABLE User( commonName CHAR(80) ,uid CHAR(10) PRIMARY KEY);-- Persistent record (see Durability in ACID)INSERT INTO User VALUES('Joe Bix', 'bix');

Page 5: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OS

Page 6: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

Page 7: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

Page 8: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

Page 9: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Page 10: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Java-app creates

User

Page 11: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Java-app creates

User

Joe Bix

bix

load

Page 12: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Java-app creates

User

Joe Bix

bix

load

termi-nation

jre

Page 13: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Java-app creates

User

Joe Bix

bix

load

termi-nation

jre

Transient Data

Transient Data

Page 14: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Persisting transient User instances

OSbix

Joe Bixcreatesapp

Java-Userspawns

jre 1

(Joe Bix,bix)RDBMS

save

termi-nation

jre

spawns

OSjre 2

Java-app creates

User

Joe Bix

bix

load

termi-nation

jre

Transient Data

Transient Data

Persistent Data

Page 15: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Observations• Processes in disjoint address spaces:

1. JRE™ runtime.

2. RDBMS server.

• Multiple runtimes possible (PHP)

• “save” and “load” denote communications across OS boundaries.

Page 16: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server

Page 17: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

Page 18: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Page 19: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Java GUI,

Applet

Page 20: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Java GUI,

Applet

JDBC

Page 21: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Java GUI,

Applet

JDBC ODBC

Exel, Word...

Page 22: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Java GUI,

Applet

JDBC ODBC

Exel, Word...

C-API

SQL Terminal

Page 23: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Networking between clients and database server

RDBMS

Server Client

TCP/IP

Java GUI,

Applet

JDBC ODBC

Exel, Word...

C-API

SQL Terminal

Proprietary protocol e.g. sockets

Page 24: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ features• Protocol connecting database client and server.

• Vendor dependent implementations.

Page 25: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ in a three-tier application

Presentation layer

Page 26: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ in a three-tier application

Presentation layer

Business logic

Page 27: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ in a three-tier application

Presentation layer

Business logic

Java FX JSF

Page 28: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ in a three-tier application

Presentation layer

Business logic

Java FX JSF

Database server

Page 29: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ in a three-tier application

Presentation layer

Business logic

Java FX JSF

Database server

JDBC

Page 30: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Page 31: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Business logic

Page 32: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Business logic

JSFJava FX

Page 33: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Business logic

JSFJava FX

JPA

Hibernate, Toplink

Page 34: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Business logic

JSFJava FX

JPA

Hibernate, Toplink

Database server

Page 35: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connecting application server and database.

Presentation layer

Business logic

JSFJava FX

JPA

Hibernate, Toplink

Database server

JDBC

Page 36: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connection parameter1. Database server type i.e. Oracle, DB2, Informix, Postgresql™, Mysql etc. due to vendor specific JDBC™ protocol implementations.

2. Server DNS name or IP number.

3. Server service's port number.

4. The database name within the given server.

5. Optional: A database user's account name and password.

Page 37: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /

Page 38: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /jdbc:mysql

1. Protocol / sub protocol definition

Page 39: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /jdbc:mysql

1. Protocol / sub protocol definition

srv.company.com

Server's DNS name or IP-address2.

Page 40: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /jdbc:mysql

1. Protocol / sub protocol definition

srv.company.com

Server's DNS name or IP-address2.

3306

TCP service's port number3.

Page 41: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /jdbc:mysql

1. Protocol / sub protocol definition

srv.company.com

Server's DNS name or IP-address2.

3306

TCP service's port number3.

foo

Database within server4.

Page 42: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Components of a JDBC™ URL

3306 foojdbc:mysql :// srv.company.com : /jdbc:mysql

1. Protocol / sub protocol definition

srv.company.com

Server's DNS name or IP-address2.

3306

TCP service's port number3.

foo

Database within server4.

URL syntax: http://www.ietf.org/rfc/rfc2396.txt

Page 43: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

IETF Uniform Resource Identifierhttps://www.ietf.org/rfc/rfc2396.txt:

absoluteURI = scheme ":" ( hier_part | opaque_part )

hier_part = ( net_path | abs_path ) [ "?" query ]

net_path = "//" authority [ abs_path ]

abs_path = "/" path_segments...

Page 44: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

URL examples• http://www.hdm-stuttgart.de/aaa

• http://someserver.com:8080/someResource

Non-standard port 8080

• ftp://mirror.mi.hdm-stuttgart.de/Firmen

Page 45: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Sub protocol examplesDatabase JDBC™ URI

PostgreSQL jdbc:postgresql://<HOST>:<PORT>/[database]

MySQL jdbc:mysql://[host][:port]/[database][?p1=v1]...

Oracle jdbc:oracle:thin:[user/password]@[host][:port]:SID

DB2 jdbc:db2://<HOST>:<PORT>/[database]

Derby jdbc:derby://[host][:port]/[database]

MS. SQL S. jdbc:sqlserver://host[:port];user=xxx;password=xyz

Sybase jdbc:sybase:Tds:<HOST>:<PORT>/[database]

Page 46: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

No standard port assignments ...Postgresql: 5432

IBM DB2: 50000

Oracle: 1521

• No official IETF standard port assignments

• Vendor specific defaults

• Explicit port specification required

Page 47: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

... but Postgresql made it into Linux>grep postgresql /etc/services postgresql 5432/tcp postgres # PostgreSQL Databasepostgresql 5432/udp postgres

Page 48: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

Page 49: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Page 50: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Page 51: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Connection

Page 52: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Connection

Statement

Page 53: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Connection

Statement

ResultSet

Page 54: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Connection

Statement

ResultSet

Driver implementation

Connection

Statement

ResultSet

Oracle

Page 55: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ architecture

Java Application

DriverManager

Driver implementation

Mysql

Connection

Statement

ResultSet

Driver implementation

Connection

Statement

ResultSet

Oracle

...

...

Informix, DB2

Sybase, ...

Page 56: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

DriverManager: Bootstrapping connections• Bootstrapping object.

• java.sql.DriverManager shipped with JRE™.

• Interfacing JRE™ and JDBC™ driver.

• Provides instances of java.sql.Connection.

• See Interfaces and classes in JDBC™.

Page 58: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Driver libraries• postgresql-42.1.4.jar

• mysql-connector-java-x.y.z.jar

• ojdbc6.jar

Page 59: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Driver libraries, Maven<groupId>postgresql</groupId><artifactId>postgresql</artifactId><version>9.1-901-1.jdbc4</version>

<groupId>com.oracle</groupId> <!-- requires access credentials --><artifactId>ojdbc7</artifactId><version>12.1.0</version>

Page 60: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Driver unavailableconn = DriverManager.getConnection( "jdbc:postgresqll://localhost/hdm", "hdmuser", "XYZ");

java.sql.SQLException: No suitable driver found for jdbc:postgresqll://localhost/hdm

at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at de.hdm_stuttgart.mi.sda1.DatabaseTest.initDatabase(DatabaseTest.java:34) ...

Page 62: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Statement interfacejava.sql.Statement Two distinct operation classes:

executeUpdate() INSERT, UPDATE, DELETE: Integer return code

executeQuery() SELECT: Returning java.sql.ResultSet, see the section called “Read Access”.

Page 63: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

Page 64: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

Page 65: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

Page 66: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

Page 67: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

Page 68: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

getStatement()

Page 69: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

getStatement()

Statement

Page 70: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

getStatement()

Statement

PreparedStatement

Page 71: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

getStatement()

Statement

PreparedStatement

ResultSet

Page 72: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ instances and relationships.

Connection

createStatement()

Statement

getConnection()

int-value

executeUpdate

executeQuery

ResultSet

getStatement()

Statement

PreparedStatement

ResultSet

Legend:

create

Reference

Page 75: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ and threading.From JDBC and Multithreading:

“Because all Oracle JDBC API methods are synchronized, if two threads try to use the connection object simultaneously, then one will beforced to wait until the other one finishes its use.”

Consequence:

• Use one java.sql.Connection per thread.

• Use connection pooling e.g. c3po.

Page 76: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ connection poolingtry (final Connection conn = C3P0DataSource.getInstance().getConnection()) {

final PreparedStatement pstmt = conn.create...; ... pstmt.executeUpdate(); // Auto close connection, back to pool.} catch (SQLException e) { e.printStackTrace();}

Page 77: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

pom.xml driver runtime scope...<dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901-1.jdbc4</version> <scope>runtime</scope></dependency> ...

Page 78: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 22: Why <scope>runtime</scope>?

Page 79: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Person tableCREATE TABLE Person ( name CHAR(20) ,email CHAR(20) UNIQUE)

Page 80: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Objective: insert person recordJava™ application executing:

INSERT INTO Person VALUES('Jim', '[email protected]')

• No database read required (No java.sql.ResultSet).

• Success / failure related database return parameter.

Page 81: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ backed data insert// Step 1: Open connection to database serverfinal Connection conn = DriverManager.getConnection ( "jdbc:postgresql://localhost/hdm", // Connection parameter URL "hdmuser", // Username "XYZ"); // Password

// Step 2: Create a Statement instancefinal Statement stmt = conn.createStatement();

// Step 3: Execute the desired INSERTfinal int updateCount = stmt.executeUpdate( "INSERT INTO Person VALUES('Jim', '[email protected]')");

// Step 4: Give feedback to the end userSystem.out.println("Successfully inserted " + updateCount + " dataset(s)");

Page 82: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

ResultExecution yields:

Successfully inserted 1 dataset(s)

Note

The database server returns the number of inserted datasets.

Page 83: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Two JDBC™ configurations1. IDE level.

2. Project level (Maven).

Page 84: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 23: Exception on inserting objects

Page 85: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Figure 18.34, “JDBC™ backed data insert” deficienciesMissing exceptionhandling:

public static void main(String[] args) throws SQLException { ...

Hard coded connectionparameters:

... = DriverManager.getConnection ( "jdbc:postgresql://localhost/hdm", //JDBC URL "hdmuser", // Username "XYZ") // Password

Page 86: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Why properties?• Connection parameter changes require recompilation!

• Parameters should be configurable.

Possible solution: Java™ properties.

Page 87: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

message.properties string externalization

print(" User name :");

...

...

Foo.java

Page 88: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

message.properties string externalization

print(" User name :");

...

...

Foo.java

User name

Page 89: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

message.properties string externalization

print(" User name :");

...

...

Foo.java

User name

User name

messages.properties

Page 90: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

message.properties string externalization

print(" User name :");

...

...

Foo.java

User name

User name

messages.propertiesPropHello.uName

Invent a key

Page 91: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

message.properties string externalization

print(" User name :");

...

...

Foo.java

User name

User name

messages.propertiesPropHello.uName

Invent a key

=

PropHello.uName

print (Messages.getString(

" "));

Foo.java

Page 92: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Properties code sketchProperties key / valuefile resources/jdbc.properties

jdbcurl=jdbc:postgresql://localhost/hdmusername=hdmuserpassword=XYZ

ResourceBundlereading properties

// resources/jdbc.propertiesResourceBundle jdbcProperties = ResourceBundle.getBundle("jdbc");

Using ResourceBundle ... Connection conn = DriverManager.getConnection( jdbcProperties.getString("jdbcurl"), jdbcProperties.getString("username"), jdbcProperties.getString("password"));

Page 94: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Database related unit test phases1. Set up: Test preparation.

• Open database connection

• Create a required schema.

• Optional: Insert initial data.

2. Test: Execute JDBC™ CRUD / SELECT operations.

3. Tear down:

• Drop schema

• Close database connection.

Page 95: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Implementing unit testspublic class InsertTest { static private Connection conn; static private Statement stmt;

@BeforeClass ❶ static public void initDatabase() throws SQLException { conn = DriverManager.getConnection( SimpleInsert.jdbcProperties.getString("jdbcurl"), SimpleInsert.jdbcProperties.getString("username"),...); ScriptUtils.executeSqlScript(conn, new ClassPathResource("schema.sql")); stmt = conn.createStatement();}

@Test ❷ public void test_010_insertJill() throws SQLException { Assert.assertEquals(1, SimpleInsert.insertPerson( stmt, "Jill", "[email protected]")); }@AfterClass ❸ static public void releaseDatabase() throws SQLException {conn.close();}

Page 96: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Spring is your friendGetting ScriptUtils.executeSqlScript(...) to work:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> <scope>test</scope></dependency>

Page 97: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Project layout

Page 98: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Closing connectionsfinal Connection conn = DriverManager.getConnection(...);... // CRUD operationsconn.close(); // Important! Wanna use a connection pool instead?

Page 99: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Employ AutoCloseableUsing try-with-resources statement.

try (final Connection conn = DriverManager.getConnection(...) { ... // CRUD operations} catch (SQLException e) {...}

Page 100: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 24: Interactive inserts, connection properties, error handling and unit testsExercise 25: Avoiding intermediate SQL file exportExercise 26: Interfaces and classes in JDBC™Exercise 27: Closing JDBC™ connectionsExercise 28: Driver dispatch mechanism

Page 101: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Sniffing a JDBC™ connection by an intruder.

DB server

TCP/IP

Page 102: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Sniffing a JDBC™ connection by an intruder.

Create int array of size 3 on heap.

DB server JDBC client

TCP/IP

Page 103: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Sniffing a JDBC™ connection by an intruder.

Create int array of size 3 on heap.

DB server JDBC clientIntruder (Wireshark)

TCP/IP

Page 104: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Setting up Wireshark• Database server and JDBC™ client on same machine.

• Connecting to the loopback (lo) interface only.

(Sufficient since client connects to localhost)

• Capture packets of type TCP having port number 3306.

Page 105: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Capturing results[...5.5.24-0ubuntu0.12.04.1.%...X*e?I1ZQ...................e,F[yoA5$T[N.mysql_native_password. A...........!.......................hdmuser ❶......U.>S.%..~h...!.xhdm............j..../*

... INSERT INTO Person VALUES('Jim', '[email protected]') ❷6... .&.#23000Duplicate entry '[email protected]' for key 'email' ❸

❶ username initiating database connection.❷ INSERT(...) statement.❸ Resulting error message sent back to the client.

Password?

Page 106: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Mysql™ securityWhat about the missing password?

Making MySQL Secure Against Attackers:

When you connect to a MySQL server, you should use a password.

The password is not transmitted in clear text over the connection.

Page 107: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Mysql™ security• Data exchange client to server nearly fully disclosed.

• Mysql mitigates the attack type's severity

• Possible solutions:

• Encrypted tunnel between client and server: like e.g. ssh port forwarding or VPN.

• Use JDBC™ driver supporting TLS.

• Irrelevant e.g. within DMZ.

Page 108: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Assembling SQL

Jim

INSERT INTO Person VALUES('Jim')

Assembling SQL

GUI Window Name:

Page 109: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection principle

Jim'); DROP TABLE Person; INSERT INTO Person VALUES('Joe

INSERT INTO PersonVALUES(' ')

SQL inject

GUI Window

Name:

Page 110: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection principle

Jim'); DROP TABLE Person; INSERT INTO Person VALUES('Joe

INSERT INTO PersonVALUES('Jim'); DROP TABLE Person; INSERT INTO Person VALUES('Joe')

GUI Window

Name:

Page 111: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Preventing traffic tickets

Page 112: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Trouble at school

Page 113: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection impact

Heartland Payment Systems data breach

Page 114: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection impact

Heartland Payment Systems data breach

March 2008

Page 115: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection impact

Heartland Payment Systems data breach

March 2008

134 million credit cards exposed through

SQL injection to install spyware

Page 116: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection impact

Heartland Payment Systems data breach

March 2008

134 million credit cards exposed through

SQL injection to install spyware

Cost: At least $129 million

Page 117: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection impact

Heartland Payment Systems data breach

March 2008

134 million credit cards exposed through

SQL injection to install spyware

Cost: At least $129 million

The vulnerability to SQL injection was well

retailers about it for several years.

understood and security analysts had warned

Page 118: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL injection relevance, [Clarke2009]Many people say they know what SQL injection is, but all they have heard about or experienced are trivial examples.

SQL injection is one of the most devastating vulnerabilities to impact a business, as it can lead to exposure of all of the sensitiveinformation stored in an application's database, including handy information such as usernames, passwords, names, addresses, phonenumbers, and credit card details.

Page 120: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 29: Attack from the dark side

Page 121: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Validating user input prior to dynamically composing SQL statements.

JimName:

GUI window

Page 122: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Validating user input prior to dynamically composing SQL statements.

JimName:

GUI window

Validinput?

Page 123: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Validating user input prior to dynamically composing SQL statements.

JimName:

GUI window

Validinput?

Noraiseerror

Page 124: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Validating user input prior to dynamically composing SQL statements.

JimName:

GUI window

Validinput?

Noraiseerror

')VALUES(' JimINSERT INTO Person

yes

Page 125: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 30: Using regular expressions in Java™Exercise 31: Input validation by regular expressions

Page 126: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL statements in Java™ applications get parsed at the database server

Application

Page 127: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL statements in Java™ applications get parsed at the database server

Application RDBMS

Page 128: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL statements in Java™ applications get parsed at the database server

Application RDBMS

INSERT INTO ...VALUES('Jim',

'[email protected]')

Page 129: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL statements in Java™ applications get parsed at the database server

Application RDBMS

INSERT INTO ...VALUES('Jim',

'[email protected]')

Interpreter

JDBC Statement

Page 130: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

SQL statements in Java™ applications get parsed at the database server

Application RDBMS

INSERT INTO ...VALUES('Jim',

'[email protected]')

Interpreter

JDBC Statement

Low-level

calls, AST

Page 131: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Two questions1. What happens when executing structural identical SQL statements (differing only by attribute values) repeatedly?

E.g. inserting thousands of records of identical structure.

2. Is this architecture adequate with respect to security concerns?

Page 132: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Addressing performanceINSERT INTO Person VALUES ('Jim', '[email protected]')INSERT INTO Person VALUES ('Eve', '[email protected]')INSERT INTO Person VALUES ('Pete', '[email protected]')...

Wasting time parsing SQL over and over again!

Page 133: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Addressing performance mitigationINSERT INTO Person VALUES ('Jim', '[email protected]'), ('Eve', '[email protected]'), ('Pete', '[email protected]') ... ;

Dealing with large record counts even this option may become questionable.

Page 134: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Addressing securityThe database server's interpreter may interpret an attacker's malicious code among with intended SQL.

Page 135: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Solution: Use java.sql.PreparedStatement• Parsing happens only once.

• Reuse per record.

• Avoids parsing contained «payload» / user input.

Page 136: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application

Page 137: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

Page 138: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?

Page 139: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

Page 140: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

Page 141: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

Page 142: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1

Page 143: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1 2

Page 144: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1 2

p.setString(1, "Jim") "Jim"

Jim

1

Page 145: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1 2

p.setString(1, "Jim") "Jim"

Jim

1

p.setString(2, "[email protected]") 2 "[email protected]"

[email protected]

Page 146: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1 2

p.setString(1, "Jim") "Jim"

Jim

1

p.setString(2, "[email protected]") 2 "[email protected]"

[email protected]

Finally:

Page 147: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement principle.

Application RDBMS

VALUES (?, ?)

INSERT INTO ...

?, ?Interpreter

PreparedStatement

AST

2 Placeholders

1 2

p.setString(1, "Jim") "Jim"

Jim

1

p.setString(2, "[email protected]") 2 "[email protected]"

[email protected]

Finally:

p.executeUpdate()

Page 148: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Three phases using parameterized queries1. PreparedStatement instance creation: Parsing SQL statement possibly containing place holders.

2. Set values of all placeholder values: No SQL parsing happens.

3. Execute the statement.

Steps 2. and 3. may be repeated without requiring re-parsing SQL statements thus saving database server resources.

Page 149: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

PreparedStatement examplefinal Connection conn = DriverManager.getConnection (...

final PreparedStatement pStmt = conn.prepareStatement( "INSERT INTO Person VALUES(?, ?)");❶

pStmt.setString(1, "Jim");❷

pStmt.setString(2, "[email protected]");❸

final int updateCount = pStmt.executeUpdate();❹

System.out.println("Successfully inserted " + updateCount + " dataset(s)");

Page 150: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Injection attempt exampleJim', '[email protected]');DROP TABLE Person;INSERT INTO Person VALUES('Joe

Attacker's injection text simply becomes part of the database server's content.

Problem solved!

Page 151: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

No dynamic table supportPreparedSatatement statement = connection.prepareStatement("SELECT ? ❶ from ?" ❷);statement.setString(1, "birthday") ❸;statement.setString(2, "Persons") ❹;ResultSet rs = statement.executeQuery() ❺;

In a nutshell: Only attribute value literals may be parameterized.

Page 152: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 32: Prepared Statements to keep the barbarians at the gate

Page 153: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ read and write• CREATE / UPDATE / DELETE

client modifies database server data:

int result = statement.executeUpdate("UPDATE Person ...");

• SELECT

client receives copies of database server data:

ResultSet result = statement.executeQuery("SELECT ... FROM Person ...");

Page 154: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Page 155: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Client

time

SELECT

transient copySam Bown

Page 156: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Client

time

SELECT

transient copySam Bown

Sam Brown r

correction

transient copy

(modified)

Page 157: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Client

time

SELECT

transient copySam Bown

Sam Brown r

correction

transient copy

(modified)UPDATE

Sam Brown

Page 158: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Client

time

SELECT

transient copySam Bown

Sam Brown r

correction

transient copy

(modified)UPDATE

Sam Brown

Persistent data

(ACID) D

Page 159: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Server / client object's life cycle

DB Server

Sam Bown

Client

time

SELECT

transient copySam Bown

Sam Brown r

correction

transient copy

(modified)UPDATE

Sam Brown

Persistent data

(ACID) D

Transient data

Page 160: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ record container• No standard Collections container e.g. java.util.List.

• “Own” container standard java.sql.ResultSet holding transient database.

Page 161: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

Page 162: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

Page 163: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Page 164: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

Page 165: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Page 166: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Page 167: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

Page 168: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

...

Eve

Page 169: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

...

Eve

...

Mick

Page 170: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

...

Eve

...

MickMick

Page 171: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

...

Eve

...

MickMick

Persistent data

Page 172: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Reading data from a database server.

Client

executeQuery(

"Select *

FROM Names")

RDBMS Server

Query

Names

Jim

...

Mick

Eve

Snapshot

Response,

Data copy,

ResultSet

Mick

...

Eve

Jimnext()

Jim

Eve

Jim

...

Eve

...

MickMick

Persistent data Transient data

Page 173: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Names and birth dates of friendsCREATE TABLE Friends ( id INTEGER NOT NULL PRIMARY KEY ,nickname char(10) ,birthdate DATE);

INSERT INTO Friends VALUES (1, 'Jim', '1991-10-10') ,(2, 'Eve', '2003-05-24') ,(3, 'Mick','2001-12-30') ;

Page 174: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Accessing friend's database recordsfinal Connection conn = DriverManager.getConnection (...);final Statement stmt = conn.createStatement();// Step 3: Creating the client side JDBC container holding our data recordsfinal ResultSet data = stmt.executeQuery("SELECT * FROM Friends"); ❶

// Step 4: Dataset iterationwhile (data.next()) { ❷

System.out.println(data.getInt("id") ❸

+ ", " + data.getString("nickname") ❹

+ ", " + data.getString("birthdate")); ❺}

Page 175: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Important ResultSet statesNew: resultSet =statement.executeQuery(...)

Caution: Data not yet accessible!

Cursor positioned:resultSet.next()returning true

Data accessible until resultSet.next()returns false.

Closed:resultSet.next()returning false

Caution: Data not longer accessible!

Page 176: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

JDBC™ to Java™ type conversionsJDBC™ Type Java™ type

CHAR, VARCHAR, LONGVARCHAR StringNUMERIC, DECIMAL java.math.BigDecimalBIT booleanTINYINT byte... ...

Shamelessly copied from JDBC Types Mapped to Java Types.

Page 177: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Java™ to JDBC™ type conversionsJava™ Type JDBC™ type

String CHAR, VARCHAR, LONGVARCHARjava.math.BigDecimal NUMERICboolean BIT... ...

Shamelessly copied from Java Types Mapped to JDBC Types.

Page 178: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Fixed type accessorsint getInt(int columnIndex)double getDouble(int columnIndex)Date getDate(int columnIndex)...

Page 179: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Polymorphic accessorsObject getObject(int columnIndex)

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

Page 180: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Access by column namefinal int id = resultSet.getInt("id");final String nickName = resultSet.getString("nickname");final Date birthDate = resultSet.getDate("birthdate");

CREATE TABLE Friends ( id INTEGER NOT NULL PRIMARY KEY ,nickname char(10) ,birthdate DATE);

Page 181: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Access by column indexfinal int id = resultSet.getInt(1);final String nickName = resultSet.getString(2);final Date birthDate = resultSet.getDate(3);

CREATE TABLE Friends ( id INTEGER NOT NULL PRIMARY KEY ,nickname char(10) ,birthdate DATE);

Page 182: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 33: Getter methods and type conversion

Page 183: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Problem: null value ambiguityfinal int count = resultSet.getInt("numProducts");

Problem: Two possibilities in case of count == 0:

1. DB attribute numProducts is 0 (zero).

2. DB attribute numProducts is null.

Page 184: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Resolving null value ambiguityfinal int count = resultSet.getInt("numProducts");

if (resultSet.wasNull()) {...} else {...}

See wasNull().

Page 185: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 34: Handling NULL values.Exercise 35: Reversing XML to RdbmsExercise 36: Generating HTML from both XML and relational input data

Page 186: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Users and groups

Stringgid

intgidNumber

StringcommonName

Group

Stringuid

intuidNumber

StringcommonName

GroupprimaryGroup

User

1

1

Page 187: Prerequisite knowledge • SQL, SQL - DML: PRIMARY KEY ...€¦ · Prerequisite knowledge • RDBMS schema and SQL - DDL: PRIMARY KEY, UNIQUE, FOREIGN KEY, NOT NULL, datatypes. •

Related exercisesExercise 37: JDBC™ and transactionsExercise 37: Isolation level 1 vs. 2Exercise 39: Aborted transactions