Search in sources :

Example 6 with AUTHOR

use of org.jooq.example.db.h2.Tables.AUTHOR in project jOOQ by jOOQ.

the class Example_2_1_ActiveRecords method run.

@Test
public void run() throws SQLException {
    Connection connection = connection();
    DSLContext dsl = DSL.using(connection);
    AuthorRecord author;
    try {
        Tools.title("Loading and changing active records");
        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());
    } finally // Don't store the changes
    {
        connection.rollback();
    }
}
Also used : AuthorRecord(org.jooq.example.db.h2.tables.records.AuthorRecord) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Example 7 with AUTHOR

use of org.jooq.example.db.h2.Tables.AUTHOR in project jOOQ by jOOQ.

the class Example_3_2_ResultSets method run.

@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);
}
Also used : Record(org.jooq.Record) Arrays(java.util.Arrays) Connection(java.sql.Connection) DSL(org.jooq.impl.DSL) Tools(org.jooq.academy.tools.Tools) Test(org.junit.Test) Tools.connection(org.jooq.academy.tools.Tools.connection) AUTHOR(org.jooq.example.db.h2.Tables.AUTHOR) Connection(java.sql.Connection) Record(org.jooq.Record) Test(org.junit.Test)

Example 8 with AUTHOR

use of org.jooq.example.db.h2.Tables.AUTHOR in project jOOQ by jOOQ.

the class Example_4_4_ExecuteListener method run.

@Test
public void run() {
    Tools.title("Displaying execution time using a custom ExecuteListener");
    ExecuteListener listener = new CallbackExecuteListener().onStart(ctx -> {
        ctx.data("time", System.nanoTime());
    }).onEnd(ctx -> {
        Long time = (Long) ctx.data("time");
        System.out.println("Execution time : " + ((System.nanoTime() - time) / 1000 / 1000.0) + "ms. Query : " + ctx.sql());
    });
    DSL.using(new DefaultConfiguration().set(SQLDialect.H2).set(new DefaultConnectionProvider(connection())).set(new DefaultExecuteListenerProvider(listener))).select(AUTHOR.ID).from(AUTHOR).fetch();
}
Also used : DSL(org.jooq.impl.DSL) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Tools(org.jooq.academy.tools.Tools) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Test(org.junit.Test) SQLDialect(org.jooq.SQLDialect) Tools.connection(org.jooq.academy.tools.Tools.connection) AUTHOR(org.jooq.example.db.h2.Tables.AUTHOR) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 AuthorRecord (org.jooq.example.db.h2.tables.records.AuthorRecord)3 Connection (java.sql.Connection)2 DSLContext (org.jooq.DSLContext)2 Record3 (org.jooq.Record3)2 Tools (org.jooq.academy.tools.Tools)2 Tools.connection (org.jooq.academy.tools.Tools.connection)2 AUTHOR (org.jooq.example.db.h2.Tables.AUTHOR)2 Author (org.jooq.example.db.h2.tables.Author)2 Book (org.jooq.example.db.h2.tables.Book)2 BookStore (org.jooq.example.db.h2.tables.BookStore)2 BookToBookStore (org.jooq.example.db.h2.tables.BookToBookStore)2 DSL (org.jooq.impl.DSL)2 Arrays (java.util.Arrays)1 Properties (java.util.Properties)1 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)1 ExecuteListener (org.jooq.ExecuteListener)1 Record (org.jooq.Record)1 SQLDialect (org.jooq.SQLDialect)1 Author (org.jooq.example.db.h2.tables.pojos.Author)1