KEMBAR78
Best Way to Write SQL in Java | PDF
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Get Back in Control of your SQL
SQL and Java could work
together so much better if
we only let them.
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Me – @lukaseder
SQL is a device whose mystery
is only exceeded by its power!
- Founder and CEO at Data Geekery
- Oracle Java Champion
- JUG.ch Board Member
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s talk about
SQL
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
SQL is Powerful!
My Reaction when I
forget the WHERE
clause on my DELETE
statement…
With autocommit
active
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
SQL and Java – in theory
Java SQL
In this metaphor, electricity is the data (SQL) that
flows into your appliance / application (Java)
one jack one plug
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
SQL and Java – in practice
Java SQL
Images from: http://en.wikipedia.org/wiki/AC_power_plugs_and_sockets. License: public domain
one jack lots of plugs
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JDBC
PreparedStatement stmt = connection.prepareStatement(
"SELECT text FROM products WHERE cust_id = ? AND value < ?");
stmt.setInt(1, custID);
stmt.setBigDecimal(2, BigDecimal.ZERO);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("TEXT"));
}
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JDBC – the naked truth
01: PreparedStatement stmt = connection.prepareStatement(
02: "SELECT p.text txt" +
03: (isAccount ? ", NVL(a.type, ?) " : "") +
04: "FROM products p " +
05: (isAccount ? " INNER JOIN accounts a USING (prod_id) " : "") +
06: " WHERE p.cust_id = ? AND p.value < ?" +
07: (isAccount ? " AND a.type LIKE '%" + type + "%'" : ""));
08: stmt.setInt(1, defaultType);
09: stmt.setInt(2, custID);
10: stmt.setBigDecimal(3, BigDecimal.ZERO);
11: ResultSet rs = stmt.executeQuery();
12:
13: while (rs.next()) {
14: Clob clob = rs.getClob("TEXT");
15: System.out.println(clob.getSubString(1, (int) clob.length()));
16: }
17:
18: rs.close();
19: stmt.close();
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JDBC – the naked truth
01: PreparedStatement stmt = connection.prepareStatement( //
02: "SELECT p.text txt" + //
03: (isAccount ? ", NVL(a.type, ?) " : "") + //
04: "FROM products p " + // Syntax error when isAccount == false
05: (isAccount ? " INNER JOIN accounts a USING (prod_id) " : "") + //
06: " WHERE p.cust_id = ? AND p.value < ?" + //
07: (isAccount ? " AND a.type LIKE '%" + type + "%'" : "")); // Syntax error and SQL injection possible
08: stmt.setInt(1, defaultType); // Wrong bind index
09: stmt.setInt(2, custID); //
10: stmt.setBigDecimal(3, BigDecimal.ZERO); //
11: ResultSet rs = stmt.executeQuery(); //
12:
13: while (rs.next()) { //
14: Clob clob = rs.getClob("TEXT"); // Wrong column name
15: System.out.println(clob.getSubString(1, (int) clob.length())); // ojdbc6: clob.free() should be called
16: } //
17:
18: rs.close(); // close() not really in finally block
19: stmt.close(); //
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
What JDBC means for developers
Images from Flickr. To the left by: Matthew Straubmuller, Greg Grossmeier. License: CC BY SA 2.0. Electric Engineers to the right copyright by Marco Sarli, all rights reserved.
With JDBC, your developers have to do a lot of
manual, error-prone (dangerous) and inefficient work
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 2.0 EntityBeans
public interface CustomerRequest extends EJBObject {
BigInteger getId();
String getText();
void setText(String text);
@Override
void remove();
}
public interface CustomerRequestHome extends EJBHome {
CustomerRequest create(BigInteger id);
CustomerRequest find(BigInteger id);
}
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 2.0 – the naked truth
<weblogic-enterprise-bean>
<ejb-name>com.example.CustomerRequestHome</ejb-name>
<entity-descriptor>
<pool>
<max-beans-in-free-pool>100</max-beans-in-free-pool>
</pool>
<entity-cache>
<max-beans-in-cache>500</max-beans-in-cache>
<idle-timeout-seconds>10</idle-timeout-seconds>
<concurrency-strategy>Database</concurrency-strategy>
</entity-cache>
<persistence>
<delay-updates-until-end-of-tx>True</delay-updates-until-end-of-tx>
</persistence>
<entity-clustering>
<home-is-clusterable>False</home-is-clusterable>
<home-load-algorithm>round-robin</home-load-algorithm>
</entity-clustering>
</entity-descriptor>
<transaction-descriptor/>
<enable-call-by-reference>True</enable-call-by-reference>
<jndi-name>com.example.CustomerRequestHome</jndi-name>
</weblogic-enterprise-bean>
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 2.0 – the naked truth
<pool>
<max-beans-in-free-pool>100</max-beans-in-free-pool>
</pool>
<entity-cache>
<max-beans-in-cache>500</max-beans-in-cache>
<idle-timeout-seconds>10</idle-timeout-seconds>
<concurrency-strategy>Database</concurrency-strategy>
</entity-cache>
<persistence>
<delay-updates-until-end-of-tx>True</delay-updates-…>
</persistence>
o_O
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 2.0
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JPA and EJB 3.0
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new Event("Conference", new Date());
em.persist(new Event("After Party", new Date());
List result = em.createQuery("from Event").getResultList();
for (Event event : (List<Event>) result) {
System.out.println("Event : " + event.getTitle());
}
em.getTransaction().commit();
em.close();
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 3.0 – the naked truth
@Entity @Table(name = "EVENTS")
public class Event {
private Long id;
private String title;
private Date date;
@Id @GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public Long getId() { /* … */ }
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "EVENT_DATE")
public Date getDate() { /* … */ }
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
EJB 3.0 – Yep, annotations!
@OneToMany(mappedBy = "destCustomerId")
@ManyToMany
@Fetch(FetchMode.SUBSELECT)
@JoinTable(
name = "customer_dealer_map",
joinColumns = {
@JoinColumn(name = "customer_id", referencedColumnName = "id")
},
inverseJoinColumns = {
@JoinColumn(name = "dealer_id", referencedColumnName = "id")
}
)
private Collection dealers;
Found at http://stackoverflow.com/q/17491912/521799
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JPA 3.0 Preview – Annotatiomania™
@SeveralAndThenNothing @MaybeThisDoesSomething
@TweakThisWithThat(
tweak = {
@TweakID(name = "id", preferredValue = 1839),
@TweakID(name = "test", preferredValue = 839),
@TweakID(name = "test.old", preferredValue = 34),
},
inCaseOf = {
@ConditionalXMLFiltering(run = 5),
}
)
@OneToMany @OneToManyMore @AnyOne @AnyBody @DoesThisEvenMeanAnything @DoesAnyoneEvenReadThis
@ManyToMany @Many @AnnotationsTotallyRock @DeclarativeProgrammingRules @NoMoreExplicitAlgorithms
@Fetch @FetchMany @FetchWithDiscriminator(name = "no_name")
@JoinTable(joinColumns = {
@JoinColumn(name = "customer_id", referencedColumnName = "id")
})
@PrefetchJoinWithDiscriminator @JustTrollingYouKnow @LOL
@IfJoiningAvoidHashJoins @ButUseHashJoinsWhenMoreThan(records = 1000)
@XmlDataTransformable @SpringPrefechAdapter
private Collection employees;
Might not be true
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
What’s next?
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Ask the industry experts
www.annotatiomania.com
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Albert Einstein
I don’t know with
what annotations
JPA III will ship,
but JPA IV will
ship with sticks
and stones
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
JPA 4.0 Preview
var employees;
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
What JPA means for developers…
Images from Wikimedia. License: public domain. High voltage power lines by Simon Koopmann. License: CC-BY SA 3.0
With JPA, your developers use a huge framework with
lots of complexity that can get hard to manage
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
… when developers actually wanted this
Java SQL
one jack one plug
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Note, we’re talking about SQL. Not Persistence…
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Note, we’re talking about SQL. Not Persistence…
FYI: Gavin King: Creator of Hibernate!
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL?
…
… so, should we maybe abandon SQL?
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Big Data? What is it?
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Source: www.itproportal.com
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Seriously, MongoDB?
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Who said it?
Our service ran at 99.99 percent
uptime in the first quarter of 2009,
runs more than 200 million
transactions a day, and has subsecond
response time; and we are constantly
making advances to deliver it even
faster.
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Marc Benioff – salesforce.com
Our service ran at 99.99 percent
uptime in the first quarter of 2009,
runs more than 200 million
transactions a day, and has subsecond
response time; and we are constantly
making advances to deliver it even
faster.
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Marc Benioff – salesforce.com
He’s talking about
salesforce.com’s
Oracle database.
He “invented” the
cloud
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Who said it?
• 300 TB of data files for production
DBs in total
• LHC logging database ~140TB,
expected growth up to ~70 TB /
year
• 13 Production experiments'
database ~120 TB in total
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Collecting LHC data with Oracle Exadata
• 300 TB of data files for production
DBs in total
• LHC logging database ~140TB,
expected growth up to ~70 TB /
year
• 13 Production experiments'
database ~120 TB in total
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? Stack Overflow on 1 SQL Server Instance
http://stackexchange.com/performance
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL for Big Data?
- You’re giving up on ACID
- You’re giving up on type safety
- You’re giving up on standards
- You’re giving up on tooling
- You’re giving up on relational algebra
- You haven’t asked operations
- You don’t actually have «Big Data»
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL for Big Data?
- You’re giving up on ACID
- You’re giving up on type safety
- You’re giving up on standards
- You’re giving up on tooling
- You’re giving up on relational algebra
- You haven’t asked operations
- You don’t actually have «Big Data»
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Seen at the O’Reilly Strata Conf:
History of NoSQL by Mark Madsen. Picture published by Edd Wilder-James
NoSQL? No, SQL!
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
So, let’s talk about
SQL
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
NoSQL? No, SQL!
So, let’s talk about
SQL
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT |
|------|------------|--------|
| 9997 | 2014-03-18 | 99.17 |
| 9981 | 2014-03-16 | 71.44 |
| 9979 | 2014-03-16 | -94.60 |
| 9977 | 2014-03-16 | -6.96 |
| 9971 | 2014-03-15 | -65.95 |
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | 71.44 | 19886.64 |
| 9979 | 2014-03-16 | -94.60 | 19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | +99.17 =19985.81 |
| 9981 | 2014-03-16 | 71.44 | +19886.64 |
| 9979 | 2014-03-16 | -94.60 | 19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | +71.44 =19886.64 |
| 9979 | 2014-03-16 | -94.60 | +19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | +71.44 =19886.64 | n
| 9979 | 2014-03-16 | -94.60 | +19815.20 | n+1
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |BALANCE(ROWn) = BALANCE(ROWn+1) + AMOUNT(ROWn)
BALANCE(ROWn+1) = BALANCE(ROWn) – AMOUNT(ROWn)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SELECT
t.*,
t.current_balance - NVL(
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
),
0) AS balance
FROM v_transactions t
WHERE t.account_id = 1
ORDER BY t.value_date DESC,
t.id DESC
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 -(99.17)| +19985.81 |
| 9981 | 2014-03-16 -(71.44)| 19886.64 |
| 9979 | 2014-03-16 -(-94.60)| 19815.20 |
| 9977 | 2014-03-16 | -6.96 | =19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
We can help
www.jooq.org/training
SQL Wizardry?
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
More SQL Calculations
| TEXT | VOTES | RANK | PERCENT |
|-------------|-------|------------|---------|
| jOOQ | 1383 | 1 | 32 % |
| Hibernate | 1029 | 2 | 23 % |
| EclipseLink | 881 | 3 | 20 % |
| JDBC | 533 | 4 | 12 % |
| Spring JDBC | 451 | 5 | 10 % |
Data may not be accurate…
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
More SQL Calculations
SELECT p.text,
p.votes,
DENSE_RANK() OVER (ORDER BY p.votes DESC) AS "rank",
LPAD(
(p.votes * 100 / SUM(p.votes) OVER ()) || ' %',
4, ' '
) AS "percent"
FROM poll_options p
WHERE p.poll_id = 12
ORDER BY p.votes DESC
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
The same with jOOQ
select (p.TEXT,
p.VOTES,
denseRank().over().orderBy(p.VOTES.desc()).as("rank"),
lpad(
p.VOTES.mul(100).div(sum(p.VOTES).over()).concat(" %"),
4, " "
).as("percent"))
.from (POLL_OPTIONS.as("p"))
.where (p.POLL_ID.eq(12))
.orderBy(p.VOTES.desc());
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
The same with jOOQ in Scala (!)
select (p.TEXT,
p.VOTES,
denseRank() over() orderBy(p.VOTES desc) as "rank",
lpad(
(p.VOTES * 100) / (sum(p.VOTES) over()) || " %",
4, " "
) as "percent")
from (POLL_OPTIONS as "p")
where (p.POLL_ID === 12)
orderBy (p.VOTES desc)
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
What jOOQ means for developers
Java SQL
one jack all plugs
jOOQ
one adaptor
With jOOQ, Java plugs into SQL intuitively, letting
your developers focus on business-logic again.
Images from Wikimedia. License: public domain. Travel converter by Cephira. License: CC-BY SA 3.0
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
What jOOQ means for developers
Java SQL
one jack all plugs
jOOQ
one adaptor
Images from Wikimedia. License: public domain. Travel converter by Cephira. License: CC-BY SA 3.0
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Code generation
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Code generation
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Code generation
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Use the generated code
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Auto completion on columns
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking with Java 8
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking – Opt out if you want
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Type checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – Oracle
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – SQL Server
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – SQLite
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – PostgreSQL
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – Oracle 11g
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Standardisation – DB2
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Syntax checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Syntax checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Syntax checking
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Active Records
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Active Records
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – But of course, DML too
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Stored Procedures
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Stored Procedures
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Examples – Stored Procedures
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
Marc Benioff from salesforce
All companies
benefit when they
can afford to focus
on innovation
rather than
infrastructure
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
A vision of a better SQL application
- Database first
- Performance
- Type safe JDBC
- Code Generation
- Active Records
- Stored Procedures
- SQL Transformation
- SQL Standardisation
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
«jOOQ» 10% discount code
A book recommendation
Markus Winand from
Use-The-Index-Luke.com
ROI north of 83’174%
Achieve proper indexing and
performance in popular RDBMS
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
«jOOQ» 20% discount code
A book recommendation (in the making)
Vlad Mihalcea from
vladmihalcea.com
JDBC, JPA, jOOQ
Performance, Scaling
https://leanpub.com/high-performance-java-persistence/c/jOOQ
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
And a shameless tool recommendation
Open source databases:
- Free / Apache license
Commercial databases:
- Commercial license
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
1. Everything is a table – Compare it to
Java 8
TABLE : Stream<Tuple<..>>
SELECT : map()
DISTINCT : distinct()
JOIN : flatMap()
WHERE / HAVING : filter()
GROUP BY : collect()
ORDER BY : sorted()
UNION ALL : concat()
See:
http://blog.jooq.org/2015/08/13/common-sql-clauses-and-their-equivalents-in-java-8-streams/
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
1. Everything is a table – Compare it to
Java 8
Better Streams:
https://github.com/jOOQ/jOOL
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Seq.seq(persons)
.collect(
count(),
max(Person::getAge),
min(Person::getHeight),
avg(Person::getWeight)
);
// (3, Optional[35],
// Optional[1.69], Optional[70.0])
Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Intro SQL and Java jOOQ Examples
That’s it folks
More free Java / SQL knowledge on:
• Blog: http://blog.jooq.org
• Twitter: @JavaOOQ / @lukaseder
• Newsletter: http://www.jooq.org

Best Way to Write SQL in Java

  • 1.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Get Back in Control of your SQL SQL and Java could work together so much better if we only let them.
  • 2.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Me – @lukaseder SQL is a device whose mystery is only exceeded by its power! - Founder and CEO at Data Geekery - Oracle Java Champion - JUG.ch Board Member
  • 3.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s talk about SQL
  • 4.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples SQL is Powerful! My Reaction when I forget the WHERE clause on my DELETE statement… With autocommit active
  • 5.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples SQL and Java – in theory Java SQL In this metaphor, electricity is the data (SQL) that flows into your appliance / application (Java) one jack one plug
  • 6.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples SQL and Java – in practice Java SQL Images from: http://en.wikipedia.org/wiki/AC_power_plugs_and_sockets. License: public domain one jack lots of plugs
  • 7.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JDBC PreparedStatement stmt = connection.prepareStatement( "SELECT text FROM products WHERE cust_id = ? AND value < ?"); stmt.setInt(1, custID); stmt.setBigDecimal(2, BigDecimal.ZERO); ResultSet rs = stmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString("TEXT")); }
  • 8.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JDBC – the naked truth 01: PreparedStatement stmt = connection.prepareStatement( 02: "SELECT p.text txt" + 03: (isAccount ? ", NVL(a.type, ?) " : "") + 04: "FROM products p " + 05: (isAccount ? " INNER JOIN accounts a USING (prod_id) " : "") + 06: " WHERE p.cust_id = ? AND p.value < ?" + 07: (isAccount ? " AND a.type LIKE '%" + type + "%'" : "")); 08: stmt.setInt(1, defaultType); 09: stmt.setInt(2, custID); 10: stmt.setBigDecimal(3, BigDecimal.ZERO); 11: ResultSet rs = stmt.executeQuery(); 12: 13: while (rs.next()) { 14: Clob clob = rs.getClob("TEXT"); 15: System.out.println(clob.getSubString(1, (int) clob.length())); 16: } 17: 18: rs.close(); 19: stmt.close();
  • 9.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JDBC – the naked truth 01: PreparedStatement stmt = connection.prepareStatement( // 02: "SELECT p.text txt" + // 03: (isAccount ? ", NVL(a.type, ?) " : "") + // 04: "FROM products p " + // Syntax error when isAccount == false 05: (isAccount ? " INNER JOIN accounts a USING (prod_id) " : "") + // 06: " WHERE p.cust_id = ? AND p.value < ?" + // 07: (isAccount ? " AND a.type LIKE '%" + type + "%'" : "")); // Syntax error and SQL injection possible 08: stmt.setInt(1, defaultType); // Wrong bind index 09: stmt.setInt(2, custID); // 10: stmt.setBigDecimal(3, BigDecimal.ZERO); // 11: ResultSet rs = stmt.executeQuery(); // 12: 13: while (rs.next()) { // 14: Clob clob = rs.getClob("TEXT"); // Wrong column name 15: System.out.println(clob.getSubString(1, (int) clob.length())); // ojdbc6: clob.free() should be called 16: } // 17: 18: rs.close(); // close() not really in finally block 19: stmt.close(); //
  • 10.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples What JDBC means for developers Images from Flickr. To the left by: Matthew Straubmuller, Greg Grossmeier. License: CC BY SA 2.0. Electric Engineers to the right copyright by Marco Sarli, all rights reserved. With JDBC, your developers have to do a lot of manual, error-prone (dangerous) and inefficient work
  • 11.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 2.0 EntityBeans public interface CustomerRequest extends EJBObject { BigInteger getId(); String getText(); void setText(String text); @Override void remove(); } public interface CustomerRequestHome extends EJBHome { CustomerRequest create(BigInteger id); CustomerRequest find(BigInteger id); }
  • 12.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 2.0 – the naked truth <weblogic-enterprise-bean> <ejb-name>com.example.CustomerRequestHome</ejb-name> <entity-descriptor> <pool> <max-beans-in-free-pool>100</max-beans-in-free-pool> </pool> <entity-cache> <max-beans-in-cache>500</max-beans-in-cache> <idle-timeout-seconds>10</idle-timeout-seconds> <concurrency-strategy>Database</concurrency-strategy> </entity-cache> <persistence> <delay-updates-until-end-of-tx>True</delay-updates-until-end-of-tx> </persistence> <entity-clustering> <home-is-clusterable>False</home-is-clusterable> <home-load-algorithm>round-robin</home-load-algorithm> </entity-clustering> </entity-descriptor> <transaction-descriptor/> <enable-call-by-reference>True</enable-call-by-reference> <jndi-name>com.example.CustomerRequestHome</jndi-name> </weblogic-enterprise-bean>
  • 13.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 2.0 – the naked truth <pool> <max-beans-in-free-pool>100</max-beans-in-free-pool> </pool> <entity-cache> <max-beans-in-cache>500</max-beans-in-cache> <idle-timeout-seconds>10</idle-timeout-seconds> <concurrency-strategy>Database</concurrency-strategy> </entity-cache> <persistence> <delay-updates-until-end-of-tx>True</delay-updates-…> </persistence> o_O
  • 14.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 2.0
  • 15.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JPA and EJB 3.0 EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); em.persist(new Event("Conference", new Date()); em.persist(new Event("After Party", new Date()); List result = em.createQuery("from Event").getResultList(); for (Event event : (List<Event>) result) { System.out.println("Event : " + event.getTitle()); } em.getTransaction().commit(); em.close();
  • 16.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 3.0 – the naked truth @Entity @Table(name = "EVENTS") public class Event { private Long id; private String title; private Date date; @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") public Long getId() { /* … */ } @Temporal(TemporalType.TIMESTAMP) @Column(name = "EVENT_DATE") public Date getDate() { /* … */ }
  • 17.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples EJB 3.0 – Yep, annotations! @OneToMany(mappedBy = "destCustomerId") @ManyToMany @Fetch(FetchMode.SUBSELECT) @JoinTable( name = "customer_dealer_map", joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "dealer_id", referencedColumnName = "id") } ) private Collection dealers; Found at http://stackoverflow.com/q/17491912/521799
  • 18.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JPA 3.0 Preview – Annotatiomania™ @SeveralAndThenNothing @MaybeThisDoesSomething @TweakThisWithThat( tweak = { @TweakID(name = "id", preferredValue = 1839), @TweakID(name = "test", preferredValue = 839), @TweakID(name = "test.old", preferredValue = 34), }, inCaseOf = { @ConditionalXMLFiltering(run = 5), } ) @OneToMany @OneToManyMore @AnyOne @AnyBody @DoesThisEvenMeanAnything @DoesAnyoneEvenReadThis @ManyToMany @Many @AnnotationsTotallyRock @DeclarativeProgrammingRules @NoMoreExplicitAlgorithms @Fetch @FetchMany @FetchWithDiscriminator(name = "no_name") @JoinTable(joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id") }) @PrefetchJoinWithDiscriminator @JustTrollingYouKnow @LOL @IfJoiningAvoidHashJoins @ButUseHashJoinsWhenMoreThan(records = 1000) @XmlDataTransformable @SpringPrefechAdapter private Collection employees; Might not be true
  • 19.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples What’s next?
  • 20.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Ask the industry experts www.annotatiomania.com
  • 21.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Albert Einstein I don’t know with what annotations JPA III will ship, but JPA IV will ship with sticks and stones
  • 22.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples JPA 4.0 Preview var employees;
  • 23.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples What JPA means for developers… Images from Wikimedia. License: public domain. High voltage power lines by Simon Koopmann. License: CC-BY SA 3.0 With JPA, your developers use a huge framework with lots of complexity that can get hard to manage
  • 24.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples … when developers actually wanted this Java SQL one jack one plug
  • 25.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Note, we’re talking about SQL. Not Persistence…
  • 26.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Note, we’re talking about SQL. Not Persistence… FYI: Gavin King: Creator of Hibernate!
  • 27.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? … … so, should we maybe abandon SQL?
  • 28.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Big Data? What is it?
  • 29.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples
  • 30.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Source: www.itproportal.com
  • 31.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Seriously, MongoDB?
  • 32.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Who said it? Our service ran at 99.99 percent uptime in the first quarter of 2009, runs more than 200 million transactions a day, and has subsecond response time; and we are constantly making advances to deliver it even faster.
  • 33.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Marc Benioff – salesforce.com Our service ran at 99.99 percent uptime in the first quarter of 2009, runs more than 200 million transactions a day, and has subsecond response time; and we are constantly making advances to deliver it even faster.
  • 34.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Marc Benioff – salesforce.com He’s talking about salesforce.com’s Oracle database. He “invented” the cloud
  • 35.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Who said it? • 300 TB of data files for production DBs in total • LHC logging database ~140TB, expected growth up to ~70 TB / year • 13 Production experiments' database ~120 TB in total
  • 36.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Collecting LHC data with Oracle Exadata • 300 TB of data files for production DBs in total • LHC logging database ~140TB, expected growth up to ~70 TB / year • 13 Production experiments' database ~120 TB in total
  • 37.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? Stack Overflow on 1 SQL Server Instance http://stackexchange.com/performance
  • 38.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL for Big Data? - You’re giving up on ACID - You’re giving up on type safety - You’re giving up on standards - You’re giving up on tooling - You’re giving up on relational algebra - You haven’t asked operations - You don’t actually have «Big Data»
  • 39.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL for Big Data? - You’re giving up on ACID - You’re giving up on type safety - You’re giving up on standards - You’re giving up on tooling - You’re giving up on relational algebra - You haven’t asked operations - You don’t actually have «Big Data»
  • 40.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Seen at the O’Reilly Strata Conf: History of NoSQL by Mark Madsen. Picture published by Edd Wilder-James NoSQL? No, SQL!
  • 41.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples So, let’s talk about SQL
  • 42.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples NoSQL? No, SQL! So, let’s talk about SQL
  • 43.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | |------|------------|--------| | 9997 | 2014-03-18 | 99.17 | | 9981 | 2014-03-16 | 71.44 | | 9979 | 2014-03-16 | -94.60 | | 9977 | 2014-03-16 | -6.96 | | 9971 | 2014-03-15 | -65.95 |
  • 44.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | 71.44 | 19886.64 | | 9979 | 2014-03-16 | -94.60 | 19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 45.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | +99.17 =19985.81 | | 9981 | 2014-03-16 | 71.44 | +19886.64 | | 9979 | 2014-03-16 | -94.60 | 19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 46.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | +71.44 =19886.64 | | 9979 | 2014-03-16 | -94.60 | +19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 47.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | +71.44 =19886.64 | n | 9979 | 2014-03-16 | -94.60 | +19815.20 | n+1 | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |BALANCE(ROWn) = BALANCE(ROWn+1) + AMOUNT(ROWn) BALANCE(ROWn+1) = BALANCE(ROWn) – AMOUNT(ROWn)
  • 48.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SELECT t.*, t.current_balance - NVL( SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) AS balance FROM v_transactions t WHERE t.account_id = 1 ORDER BY t.value_date DESC, t.id DESC
  • 49.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING )
  • 50.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING )
  • 51.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING )
  • 52.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING )
  • 53.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 -(99.17)| +19985.81 | | 9981 | 2014-03-16 -(71.44)| 19886.64 | | 9979 | 2014-03-16 -(-94.60)| 19815.20 | | 9977 | 2014-03-16 | -6.96 | =19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 54.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples We can help www.jooq.org/training SQL Wizardry?
  • 55.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples More SQL Calculations | TEXT | VOTES | RANK | PERCENT | |-------------|-------|------------|---------| | jOOQ | 1383 | 1 | 32 % | | Hibernate | 1029 | 2 | 23 % | | EclipseLink | 881 | 3 | 20 % | | JDBC | 533 | 4 | 12 % | | Spring JDBC | 451 | 5 | 10 % | Data may not be accurate…
  • 56.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples More SQL Calculations SELECT p.text, p.votes, DENSE_RANK() OVER (ORDER BY p.votes DESC) AS "rank", LPAD( (p.votes * 100 / SUM(p.votes) OVER ()) || ' %', 4, ' ' ) AS "percent" FROM poll_options p WHERE p.poll_id = 12 ORDER BY p.votes DESC
  • 57.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples The same with jOOQ select (p.TEXT, p.VOTES, denseRank().over().orderBy(p.VOTES.desc()).as("rank"), lpad( p.VOTES.mul(100).div(sum(p.VOTES).over()).concat(" %"), 4, " " ).as("percent")) .from (POLL_OPTIONS.as("p")) .where (p.POLL_ID.eq(12)) .orderBy(p.VOTES.desc());
  • 58.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples The same with jOOQ in Scala (!) select (p.TEXT, p.VOTES, denseRank() over() orderBy(p.VOTES desc) as "rank", lpad( (p.VOTES * 100) / (sum(p.VOTES) over()) || " %", 4, " " ) as "percent") from (POLL_OPTIONS as "p") where (p.POLL_ID === 12) orderBy (p.VOTES desc)
  • 59.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples What jOOQ means for developers Java SQL one jack all plugs jOOQ one adaptor With jOOQ, Java plugs into SQL intuitively, letting your developers focus on business-logic again. Images from Wikimedia. License: public domain. Travel converter by Cephira. License: CC-BY SA 3.0
  • 60.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples What jOOQ means for developers Java SQL one jack all plugs jOOQ one adaptor Images from Wikimedia. License: public domain. Travel converter by Cephira. License: CC-BY SA 3.0
  • 61.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples
  • 62.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples
  • 63.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Code generation
  • 64.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Code generation
  • 65.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Code generation
  • 66.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Use the generated code
  • 67.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Auto completion on columns
  • 68.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 69.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 70.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 71.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 72.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking with Java 8
  • 73.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 74.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking – Opt out if you want
  • 75.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 76.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 77.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Type checking
  • 78.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – Oracle
  • 79.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – SQL Server
  • 80.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – SQLite
  • 81.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation
  • 82.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – PostgreSQL
  • 83.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – Oracle 11g
  • 84.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation – DB2
  • 85.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Syntax checking
  • 86.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Syntax checking
  • 87.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Syntax checking
  • 88.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Active Records
  • 89.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Active Records
  • 90.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – But of course, DML too
  • 91.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Stored Procedures
  • 92.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Stored Procedures
  • 93.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Stored Procedures
  • 94.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Marc Benioff from salesforce All companies benefit when they can afford to focus on innovation rather than infrastructure
  • 95.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples A vision of a better SQL application - Database first - Performance - Type safe JDBC - Code Generation - Active Records - Stored Procedures - SQL Transformation - SQL Standardisation
  • 96.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples «jOOQ» 10% discount code A book recommendation Markus Winand from Use-The-Index-Luke.com ROI north of 83’174% Achieve proper indexing and performance in popular RDBMS
  • 97.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples «jOOQ» 20% discount code A book recommendation (in the making) Vlad Mihalcea from vladmihalcea.com JDBC, JPA, jOOQ Performance, Scaling https://leanpub.com/high-performance-java-persistence/c/jOOQ
  • 98.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples And a shameless tool recommendation Open source databases: - Free / Apache license Commercial databases: - Commercial license
  • 99.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples 1. Everything is a table – Compare it to Java 8 TABLE : Stream<Tuple<..>> SELECT : map() DISTINCT : distinct() JOIN : flatMap() WHERE / HAVING : filter() GROUP BY : collect() ORDER BY : sorted() UNION ALL : concat() See: http://blog.jooq.org/2015/08/13/common-sql-clauses-and-their-equivalents-in-java-8-streams/
  • 100.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples 1. Everything is a table – Compare it to Java 8 Better Streams: https://github.com/jOOQ/jOOL
  • 101.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Seq.seq(persons) .collect( count(), max(Person::getAge), min(Person::getHeight), avg(Person::getWeight) ); // (3, Optional[35], // Optional[1.69], Optional[70.0])
  • 102.
    Copyright (c) 2009-2015by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples That’s it folks More free Java / SQL knowledge on: • Blog: http://blog.jooq.org • Twitter: @JavaOOQ / @lukaseder • Newsletter: http://www.jooq.org