Search in sources :

Example 16 with Jdbi

use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.

the class JdbiTest method whenNoResults_thenFindOnlyThrows.

@Test
public void whenNoResults_thenFindOnlyThrows() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        try {
            handle.createQuery("select * from project_8").mapToMap().findOnly();
            fail("Exception expected");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 17 with Jdbi

use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.

the class JdbiTest method whenException_thenTransactionIsRolledBack.

@Test
public void whenException_thenTransactionIsRolledBack() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        try {
            handle.useTransaction(h -> {
                h.execute("create table PROJECT_14 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
                h.execute("INSERT INTO PROJECT_14 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
                List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
                assertTrue(h.isInTransaction());
                assertEquals(1, list.size());
                throw new Exception("rollback");
            });
        } catch (Exception ignored) {
        }
        List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
        assertFalse(handle.isInTransaction());
        assertEquals(0, list.size());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 18 with Jdbi

use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.

the class JdbiTest method whenNoResults_thenFindFirstReturnsNone.

@Test
public void whenNoResults_thenFindFirstReturnsNone() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        assertFalse(handle.createQuery("select * from project_6").mapToMap().findFirst().isPresent());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 19 with Jdbi

use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.

the class JdbiTest method whenJdbiCreated_thenSuccess.

@Test
public void whenJdbiCreated_thenSuccess() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    Jdbi.create("WRONG");
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 20 with Jdbi

use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.

the class JdbiTest method whenHandle_thenBoh.

@Test
public void whenHandle_thenBoh() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    final Handle[] handleRef = new Handle[1];
    boolean closed = jdbi.withHandle(handle -> {
        handleRef[0] = handle;
        return handle.isClosed();
    });
    assertFalse(closed);
    assertTrue(handleRef[0].isClosed());
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)36 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)11 Something (org.jdbi.v3.core.Something)6 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Query (org.jdbi.v3.core.statement.Query)5 Before (org.junit.Before)5 SQLException (java.sql.SQLException)4 Update (org.jdbi.v3.core.statement.Update)3 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)3 BeforeClass (org.junit.BeforeClass)3 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)2 DefaultStatementBuilder (org.jdbi.v3.core.statement.DefaultStatementBuilder)2 SqlObjects (org.jdbi.v3.sqlobject.SqlObjects)2 Bind (org.jdbi.v3.sqlobject.customizer.Bind)2 SqlQuery (org.jdbi.v3.sqlobject.statement.SqlQuery)2 Rule (org.junit.Rule)2