SlideShare a Scribd company logo
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
Ad

Recommended

Move from J2EE to Java EE
Move from J2EE to Java EE
Hirofumi Iwasaki
 
Have You Seen Java EE Lately?
Have You Seen Java EE Lately?
Reza Rahman
 
What's coming in Java EE 8
What's coming in Java EE 8
David Delabassee
 
Java EE 8 - An instant snapshot
Java EE 8 - An instant snapshot
David Delabassee
 
Java EE Revisits GoF Design Patterns
Java EE Revisits GoF Design Patterns
Murat Yener
 
Java EE 7 - Overview and Status
Java EE 7 - Overview and Status
Java Usergroup Berlin-Brandenburg
 
Finally, EE Security API JSR 375
Finally, EE Security API JSR 375
Alex Kosowski
 
Java EE 8: On the Horizon
Java EE 8: On the Horizon
Josh Juneau
 
EJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and Strategy
David Delabassee
 
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
Edward Burns
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
Reza Rahman
 
Testing Java EE Applications Using Arquillian
Testing Java EE Applications Using Arquillian
Reza Rahman
 
What's New in WebLogic 12.1.3 and Beyond
What's New in WebLogic 12.1.3 and Beyond
Oracle
 
Java EE 7 overview
Java EE 7 overview
Masoud Kalali
 
GlassFish BOF
GlassFish BOF
glassfish
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
Reza Rahman
 
Java EE 8 Recipes
Java EE 8 Recipes
Josh Juneau
 
Geecon 2017 Anatomy of Java Vulnerabilities
Geecon 2017 Anatomy of Java Vulnerabilities
Steve Poole
 
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
Steve Poole
 
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Bruno Borges
 
Java ee 8 + security overview
Java ee 8 + security overview
Rudy De Busscher
 
Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]
Ryan Cuprak
 
JavaOne 2011: Migrating Spring Applications to Java EE 6
JavaOne 2011: Migrating Spring Applications to Java EE 6
Bert Ertman
 
Ip project work test your knowledge
Ip project work test your knowledge
KïShørê Choudhary
 
Modul Kelas Programming : Java MySQL
Modul Kelas Programming : Java MySQL
FgroupIndonesia
 
Ip project visual mobile
Ip project visual mobile
Kendriya vidyalaya no.1 cantt shahjahanpur
 
informatics practices practical file
informatics practices practical file
Sai Sathvick Chirakala
 
PPT FOR ONLINE HOTEL MANAGEMENT
PPT FOR ONLINE HOTEL MANAGEMENT
Jaya0006
 
Investigatory Project
Investigatory Project
Russen Charlotte
 
Ip project
Ip project
Jasmeet Singh
 

More Related Content

What's hot (15)

EJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and Strategy
David Delabassee
 
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
Edward Burns
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
Reza Rahman
 
Testing Java EE Applications Using Arquillian
Testing Java EE Applications Using Arquillian
Reza Rahman
 
What's New in WebLogic 12.1.3 and Beyond
What's New in WebLogic 12.1.3 and Beyond
Oracle
 
Java EE 7 overview
Java EE 7 overview
Masoud Kalali
 
GlassFish BOF
GlassFish BOF
glassfish
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
Reza Rahman
 
Java EE 8 Recipes
Java EE 8 Recipes
Josh Juneau
 
Geecon 2017 Anatomy of Java Vulnerabilities
Geecon 2017 Anatomy of Java Vulnerabilities
Steve Poole
 
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
Steve Poole
 
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Bruno Borges
 
Java ee 8 + security overview
Java ee 8 + security overview
Rudy De Busscher
 
Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]
Ryan Cuprak
 
JavaOne 2011: Migrating Spring Applications to Java EE 6
JavaOne 2011: Migrating Spring Applications to Java EE 6
Bert Ertman
 
EJB and CDI - Alignment and Strategy
EJB and CDI - Alignment and Strategy
David Delabassee
 
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
HTTP/2 comes to Java. What Servlet 4.0 means to you. DevNexus 2015
Edward Burns
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
Reza Rahman
 
Testing Java EE Applications Using Arquillian
Testing Java EE Applications Using Arquillian
Reza Rahman
 
What's New in WebLogic 12.1.3 and Beyond
What's New in WebLogic 12.1.3 and Beyond
Oracle
 
GlassFish BOF
GlassFish BOF
glassfish
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
Reza Rahman
 
Java EE 8 Recipes
Java EE 8 Recipes
Josh Juneau
 
Geecon 2017 Anatomy of Java Vulnerabilities
Geecon 2017 Anatomy of Java Vulnerabilities
Steve Poole
 
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
The Anatomy of Java Vulnerabilities (Devoxx UK 2017)
Steve Poole
 
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Construindo aplicações com HTML5, WebSockets, e Java EE 7
Bruno Borges
 
Java ee 8 + security overview
Java ee 8 + security overview
Rudy De Busscher
 
Top 50 java ee 7 best practices [con5669]
Top 50 java ee 7 best practices [con5669]
Ryan Cuprak
 
JavaOne 2011: Migrating Spring Applications to Java EE 6
JavaOne 2011: Migrating Spring Applications to Java EE 6
Bert Ertman
 

Viewers also liked (8)

Ip project work test your knowledge
Ip project work test your knowledge
KïShørê Choudhary
 
Modul Kelas Programming : Java MySQL
Modul Kelas Programming : Java MySQL
FgroupIndonesia
 
Ip project visual mobile
Ip project visual mobile
Kendriya vidyalaya no.1 cantt shahjahanpur
 
informatics practices practical file
informatics practices practical file
Sai Sathvick Chirakala
 
PPT FOR ONLINE HOTEL MANAGEMENT
PPT FOR ONLINE HOTEL MANAGEMENT
Jaya0006
 
Investigatory Project
Investigatory Project
Russen Charlotte
 
Ip project
Ip project
Jasmeet Singh
 
Tic tac toe c++ project presentation
Tic tac toe c++ project presentation
Saad Symbian
 
Ad

Similar to Best Way to Write SQL in Java (20)

Get Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegree
DataGeekery
 
The Awesome jOOQ JavaOne Talk
The Awesome jOOQ JavaOne Talk
Lukas Eder
 
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
DataGeekery
 
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
DataGeekery
 
The vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQL
Lukas Eder
 
jOOQ at Topconf 2013
jOOQ at Topconf 2013
Lukas Eder
 
Get Back in Control of Your SQL with jOOQ at #Java2Days
Get Back in Control of Your SQL with jOOQ at #Java2Days
Lukas Eder
 
Get Back in Control of your SQL
Get Back in Control of your SQL
Java Usergroup Berlin-Brandenburg
 
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
JAXLondon_Conference
 
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
AamirRafique14
 
Presentation for java data base connectivity
Presentation for java data base connectivity
kanjariya006
 
Introduction to jOOQ
Introduction to jOOQ
Gonzalo Ortiz Jaureguizar
 
Database Programming Techniques
Database Programming Techniques
Raji Ghawi
 
Jdbc oracle
Jdbc oracle
yazidds2
 
Jdbc presentation
Jdbc presentation
nrjoshiee
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
JDBC Connectivity Model
JDBC Connectivity Model
kunj desai
 
Java JDBC
Java JDBC
Jussi Pohjolainen
 
Jdbc
Jdbc
Jussi Pohjolainen
 
Lecture 1. java database connectivity
Lecture 1. java database connectivity
Waheedullah Suliman Khail
 
Get Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegree
DataGeekery
 
The Awesome jOOQ JavaOne Talk
The Awesome jOOQ JavaOne Talk
Lukas Eder
 
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
DataGeekery
 
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
DataGeekery
 
The vJUG talk about jOOQ: Get Back in Control of Your SQL
The vJUG talk about jOOQ: Get Back in Control of Your SQL
Lukas Eder
 
jOOQ at Topconf 2013
jOOQ at Topconf 2013
Lukas Eder
 
Get Back in Control of Your SQL with jOOQ at #Java2Days
Get Back in Control of Your SQL with jOOQ at #Java2Days
Lukas Eder
 
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
JAXLondon_Conference
 
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
AamirRafique14
 
Presentation for java data base connectivity
Presentation for java data base connectivity
kanjariya006
 
Database Programming Techniques
Database Programming Techniques
Raji Ghawi
 
Jdbc oracle
Jdbc oracle
yazidds2
 
Jdbc presentation
Jdbc presentation
nrjoshiee
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
JDBC Connectivity Model
JDBC Connectivity Model
kunj desai
 
Ad

More from Gerger (13)

Source Control for the Oracle Database
Source Control for the Oracle Database
Gerger
 
Big Data for Oracle Professionals
Big Data for Oracle Professionals
Gerger
 
Apache Spark, the Next Generation Cluster Computing
Apache Spark, the Next Generation Cluster Computing
Gerger
 
Version control for PL/SQL
Version control for PL/SQL
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
Gerger
 
Shaping Optimizer's Search Space
Shaping Optimizer's Search Space
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
Monitoring Oracle Database Instances with Zabbix
Monitoring Oracle Database Instances with Zabbix
Gerger
 
Introducing ProHuddle
Introducing ProHuddle
Gerger
 
Use Cases of Row Pattern Matching in Oracle 12c
Use Cases of Row Pattern Matching in Oracle 12c
Gerger
 
Introducing Gitora,the version control tool for PL/SQL
Introducing Gitora,the version control tool for PL/SQL
Gerger
 
Source Control for the Oracle Database
Source Control for the Oracle Database
Gerger
 
Big Data for Oracle Professionals
Big Data for Oracle Professionals
Gerger
 
Apache Spark, the Next Generation Cluster Computing
Apache Spark, the Next Generation Cluster Computing
Gerger
 
Version control for PL/SQL
Version control for PL/SQL
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
PostgreSQL for Oracle Developers and DBA's
PostgreSQL for Oracle Developers and DBA's
Gerger
 
Shaping Optimizer's Search Space
Shaping Optimizer's Search Space
Gerger
 
Gitora, Version Control for PL/SQL
Gitora, Version Control for PL/SQL
Gerger
 
Monitoring Oracle Database Instances with Zabbix
Monitoring Oracle Database Instances with Zabbix
Gerger
 
Introducing ProHuddle
Introducing ProHuddle
Gerger
 
Use Cases of Row Pattern Matching in Oracle 12c
Use Cases of Row Pattern Matching in Oracle 12c
Gerger
 
Introducing Gitora,the version control tool for PL/SQL
Introducing Gitora,the version control tool for PL/SQL
Gerger
 

Recently uploaded (20)

Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
declaration of Variables and constants.pptx
declaration of Variables and constants.pptx
meemee7378
 
ElectraSuite_Prsentation(online voting system).pptx
ElectraSuite_Prsentation(online voting system).pptx
mrsinankhan01
 
Top Time Tracking Solutions for Accountants
Top Time Tracking Solutions for Accountants
oliviareed320
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
University Campus Navigation for All - Peak of Data & AI
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 

Best Way to Write SQL in Java

  • 1. 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.
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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")); }
  • 8. 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();
  • 9. 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(); //
  • 10. 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
  • 11. 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); }
  • 12. 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>
  • 13. 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
  • 14. 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
  • 15. 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();
  • 16. 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() { /* … */ }
  • 17. 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
  • 18. 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
  • 19. 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?
  • 20. 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
  • 21. 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
  • 22. 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;
  • 23. 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
  • 24. 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
  • 25. 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…
  • 26. 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!
  • 27. 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?
  • 28. 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?
  • 29. Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples
  • 30. 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
  • 31. Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Seriously, MongoDB?
  • 32. 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.
  • 33. 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.
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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»
  • 39. 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»
  • 40. 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!
  • 41. 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
  • 42. 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
  • 43. 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 |
  • 44. 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 |
  • 45. 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 |
  • 46. 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 |
  • 47. 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)
  • 48. 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
  • 49. 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 )
  • 50. 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 )
  • 51. 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 )
  • 52. 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 )
  • 53. 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 |
  • 54. 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?
  • 55. 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…
  • 56. 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
  • 57. 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());
  • 58. 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)
  • 59. 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
  • 60. 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
  • 61. Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples
  • 62. Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples
  • 63. 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
  • 64. 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
  • 65. 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
  • 66. 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
  • 67. 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
  • 68. 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
  • 69. 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
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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
  • 76. 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
  • 77. 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
  • 78. 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
  • 79. 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
  • 80. 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
  • 81. Copyright (c) 2009-2015 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Intro SQL and Java jOOQ Examples Examples – Standardisation
  • 82. 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
  • 83. 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
  • 84. 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
  • 85. 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
  • 86. 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
  • 87. 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
  • 88. 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
  • 89. 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
  • 90. 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
  • 91. 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
  • 92. 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
  • 93. 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
  • 94. 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
  • 95. 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
  • 96. 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
  • 97. 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
  • 98. 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
  • 99. 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/
  • 100. 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
  • 101. 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])
  • 102. 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