From afbb924beedfccc2750fdff8b4e780446062b875 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 17 Sep 2014 18:37:34 +0200 Subject: [PATCH] More jOOQ Academy content --- .../jOOQ-academy/src/main/resources/log4j.xml | 22 +---- .../section1/Example_1_1_PrintTheQuery.java | 4 +- .../section1/Example_1_2_ExecuteTheQuery.java | 4 +- .../section1/Example_1_4_Predicates.java | 14 +++- .../Example_1_5_ColumnExpressions.java | 4 +- .../section2/Example_2_1_ActiveRecords.java | 34 +++++++- .../Example_3_1_CheckedExceptions.java | 83 +++++++++++++++++++ .../section3/Example_3_2_ResultSets.java | 78 +++++++++++++++++ 8 files changed, 207 insertions(+), 36 deletions(-) create mode 100644 jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_1_CheckedExceptions.java create mode 100644 jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_2_ResultSets.java diff --git a/jOOQ-examples/jOOQ-academy/src/main/resources/log4j.xml b/jOOQ-examples/jOOQ-academy/src/main/resources/log4j.xml index 8c478c982b..a8921d66aa 100644 --- a/jOOQ-examples/jOOQ-academy/src/main/resources/log4j.xml +++ b/jOOQ-examples/jOOQ-academy/src/main/resources/log4j.xml @@ -9,28 +9,8 @@ - - - - - - - - - - - - - - - - - - - - - + diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_1_PrintTheQuery.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_1_PrintTheQuery.java index cb73d206b6..16afc9acb1 100644 --- a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_1_PrintTheQuery.java +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_1_PrintTheQuery.java @@ -43,8 +43,6 @@ package org.jooq.academy.section1; import static org.jooq.example.db.h2.Tables.AUTHOR; import static org.jooq.impl.DSL.select; -import java.sql.SQLException; - import org.jooq.academy.tools.Tools; import org.junit.Test; @@ -52,7 +50,7 @@ import org.junit.Test; public class Example_1_1_PrintTheQuery { @Test - public void run() throws SQLException { + public void run() { // This creates a simple query without executing it // By default, a Query's toString() method will print the SQL string to the console diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_2_ExecuteTheQuery.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_2_ExecuteTheQuery.java index b41cb169b8..318286d77d 100644 --- a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_2_ExecuteTheQuery.java +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_2_ExecuteTheQuery.java @@ -43,8 +43,6 @@ package org.jooq.academy.section1; import static org.jooq.academy.tools.Tools.connection; import static org.jooq.example.db.h2.Tables.AUTHOR; -import java.sql.SQLException; - import org.jooq.academy.tools.Tools; import org.jooq.impl.DSL; @@ -53,7 +51,7 @@ import org.junit.Test; public class Example_1_2_ExecuteTheQuery { @Test - public void run() throws SQLException { + public void run() { // All we need to execute a query is provide it with a connection and then // call fetch() on it. diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_4_Predicates.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_4_Predicates.java index bda5a40ebf..5c1c27959d 100644 --- a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_4_Predicates.java +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_4_Predicates.java @@ -45,8 +45,6 @@ import static org.jooq.example.db.h2.Tables.AUTHOR; import static org.jooq.example.db.h2.Tables.BOOK; import static org.jooq.impl.DSL.select; -import java.sql.SQLException; - import org.jooq.DSLContext; import org.jooq.academy.tools.Tools; import org.jooq.impl.DSL; @@ -56,7 +54,7 @@ import org.junit.Test; public class Example_1_4_Predicates { @Test - public void run() throws SQLException { + public void run() { DSLContext dsl = DSL.using(connection()); Tools.title("Combine predicates using AND"); @@ -85,6 +83,16 @@ public class Example_1_4_Predicates { .fetch() ); + /* + Tools.title("Wrong type of columns in subquery"); + Tools.print( + dsl.select() + .from(AUTHOR) + .where(AUTHOR.ID.in(select(BOOK.TITLE).from(BOOK))) + .fetch() + ); + */ + /* Tools.title("Wrong number of columns in subquery"); Tools.print( diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_5_ColumnExpressions.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_5_ColumnExpressions.java index e2e508b9c5..74f6e0de3e 100644 --- a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_5_ColumnExpressions.java +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section1/Example_1_5_ColumnExpressions.java @@ -45,8 +45,6 @@ import static org.jooq.example.db.h2.Tables.AUTHOR; import static org.jooq.impl.DSL.concat; import static org.jooq.impl.DSL.val; -import java.sql.SQLException; - import org.jooq.DSLContext; import org.jooq.academy.tools.Tools; import org.jooq.impl.DSL; @@ -56,7 +54,7 @@ import org.junit.Test; public class Example_1_5_ColumnExpressions { @Test - public void run() throws SQLException { + public void run() { DSLContext dsl = DSL.using(connection()); Tools.title("CONCAT() function with prefix notation"); diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section2/Example_2_1_ActiveRecords.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section2/Example_2_1_ActiveRecords.java index 72ed9d2fbe..d4f04e87fb 100644 --- a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section2/Example_2_1_ActiveRecords.java +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section2/Example_2_1_ActiveRecords.java @@ -61,14 +61,42 @@ public class Example_2_1_ActiveRecords { Connection connection = connection(); DSLContext dsl = DSL.using(connection); + AuthorRecord author; + try { Tools.title("Loading and changing active records"); - - AuthorRecord author = dsl.selectFrom(AUTHOR).where(AUTHOR.ID.eq(1)).fetchOne(); + author = dsl.selectFrom(AUTHOR).where(AUTHOR.ID.eq(1)).fetchOne(); author.setDateOfBirth(Date.valueOf("2000-01-01")); author.store(); - Tools.print(author); + + + Tools.title("Creating a new active record"); + author = dsl.newRecord(AUTHOR); + author.setId(3); + author.setFirstName("Alfred"); + author.setLastName("Hitchcock"); + author.store(); + Tools.print(author); + + + Tools.title("Refreshing an active record"); + author = dsl.newRecord(AUTHOR); + author.setId(3); + author.refresh(); + Tools.print(author); + + + Tools.title("Updating an active record"); + author.setDateOfBirth(Date.valueOf("1899-08-13")); + author.store(); + Tools.print(author); + + + Tools.title("Deleting an active record"); + author.delete(); + Tools.print(dsl.selectFrom(AUTHOR).fetch()); + } // Don't store the changes diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_1_CheckedExceptions.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_1_CheckedExceptions.java new file mode 100644 index 0000000000..bcf6075a40 --- /dev/null +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_1_CheckedExceptions.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.academy.section3; + +import static org.jooq.academy.tools.Tools.connection; +import static org.jooq.example.db.h2.Tables.AUTHOR; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.jooq.academy.tools.Tools; +import org.jooq.impl.DSL; + +import org.junit.Test; + +public class Example_3_1_CheckedExceptions { + + @Test + public void run() throws SQLException { + Connection connection = connection(); + + Tools.title("JDBC throws lots of checked exceptions"); + + // These two calls can throw a SQLException + try (PreparedStatement stmt = connection.prepareStatement("SELECT FIRST_NAME FROM AUTHOR"); + ResultSet rs = stmt.executeQuery()) { + + // This can throw a SQLException + while (rs.next()) { + + // This can throw a SQLException + System.out.println(rs.getString(1)); + } + } + + Tools.title("jOOQ doesn't throw any checked exceptions"); + DSL.using(connection) + .select(AUTHOR.FIRST_NAME) + .from(AUTHOR) + .fetch() + .forEach(record -> System.out.println(record.getValue(AUTHOR.FIRST_NAME))); + } +} diff --git a/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_2_ResultSets.java b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_2_ResultSets.java new file mode 100644 index 0000000000..c63690f851 --- /dev/null +++ b/jOOQ-examples/jOOQ-academy/src/test/java/org/jooq/academy/section3/Example_3_2_ResultSets.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * This work is dual-licensed + * - under the Apache Software License 2.0 (the "ASL") + * - under the jOOQ License and Maintenance Agreement (the "jOOQ License") + * ============================================================================= + * You may choose which license applies to you: + * + * - If you're using this work with Open Source databases, you may choose + * either ASL or jOOQ License. + * - If you're using this work with at least one commercial database, you must + * choose jOOQ License + * + * For more information, please visit http://www.jooq.org/licenses + * + * Apache Software License 2.0: + * ----------------------------------------------------------------------------- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * jOOQ License and Maintenance Agreement: + * ----------------------------------------------------------------------------- + * Data Geekery grants the Customer the non-exclusive, timely limited and + * non-transferable license to install and use the Software under the terms of + * the jOOQ License and Maintenance Agreement. + * + * This library is distributed with a LIMITED WARRANTY. See the jOOQ License + * and Maintenance Agreement for more details: http://www.jooq.org/licensing + */ +package org.jooq.academy.section3; + +import static org.jooq.academy.tools.Tools.connection; +import static org.jooq.example.db.h2.Tables.AUTHOR; + +import java.sql.Connection; +import java.util.Arrays; + +import org.jooq.Record; +import org.jooq.academy.tools.Tools; +import org.jooq.impl.DSL; + +import org.junit.Test; + +public class Example_3_2_ResultSets { + + @Test + public void run() { + Connection connection = connection(); + + Tools.title("Using jOOQ Results in foreach loops"); + for (Record record : DSL.using(connection) + .select() + .from(AUTHOR) + .fetch()) { + System.out.println(record); + } + + Tools.title("Using jOOQ Results with Java 8 streams"); + DSL.using(connection) + .select() + .from(AUTHOR) + .fetch() + .stream() + .flatMap(record -> Arrays.stream(record.intoArray())) + .forEach(System.out::println); + } +}