More jOOQ Academy content
This commit is contained in:
parent
31477324dc
commit
afbb924bee
@ -9,28 +9,8 @@
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.core">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.beans">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.context">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.web">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.jooq.impl">
|
||||
<level value="debug"/>
|
||||
</logger>
|
||||
|
||||
<root>
|
||||
<priority value="debug"/>
|
||||
<priority value="info"/>
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
</log4j:configuration>
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)));
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user