Search in sources :

Example 11 with Query

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

the class JdbiTest method whenMultipleResults_thenFindOnlyThrows.

@Test
public void whenMultipleResults_thenFindOnlyThrows() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        try {
            Query query = handle.createQuery("select * from project_9");
            Map<String, Object> onlyResult = query.mapToMap().findOnly();
            fail("Exception expected");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 12 with Query

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

the class JdbiTest method whenSelectMapToString_thenResultIsString.

@Test
public void whenSelectMapToString_thenResultIsString() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        Query query = handle.createQuery("select name, url from PROJECT_4 order by id");
        List<String> list = query.mapTo(String.class).list();
        assertEquals("tutorials", list.get(0));
        assertEquals("REST with Spring", list.get(1));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) Test(org.junit.Test)

Example 13 with Query

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

the class JdbiTest method whenMultipleResults_thenFindFirstReturnsFirst.

@Test
public void whenMultipleResults_thenFindFirstReturnsFirst() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        Query query = handle.createQuery("select * from project_7 order by id");
        Optional<Map<String, Object>> first = query.mapToMap().findFirst();
        assertTrue(first.isPresent());
        assertEquals("tutorials", first.get().get("name"));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 Query (org.jdbi.v3.core.statement.Query)7 Jdbi (org.jdbi.v3.core.Jdbi)5 Something (org.jdbi.v3.core.Something)4 HashMap (java.util.HashMap)2 Handle (org.jdbi.v3.core.Handle)2 Update (org.jdbi.v3.core.statement.Update)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Type (java.lang.reflect.Type)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1