Search in sources :

Example 26 with Jdbi

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

the class TestArgumentFactory method testRegisterOnJdbi.

@Test
public void testRegisterOnJdbi() throws Exception {
    final Jdbi db = dbRule.getJdbi();
    db.registerArgument(new NameAF());
    try (Handle h = db.open()) {
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 27 with Jdbi

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

the class FiveMinuteTourTest method setUp.

@Before
public void setUp() {
    // tag::createJdbi[]
    // H2 in-memory database
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    // end::createJdbi[]
    // shared handle to keep database open
    this.jdbi = jdbi;
    this.handle = jdbi.open();
    // tag::useHandle[]
    jdbi.useHandle(handle -> {
        handle.execute("create table contacts (id int primary key, name varchar(100))");
        handle.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
        handle.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
    });
    // end::useHandle[]
    // tag::withHandle[]
    List<String> names = jdbi.withHandle(handle -> handle.createQuery("select name from contacts").mapTo(String.class).list());
    assertThat(names).contains("Alice", "Bob");
// end::withHandle[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Before(org.junit.Before)

Example 28 with Jdbi

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

the class IntroductionTest method core.

@Test
public void core() {
    // tag::core[]
    // (H2 in-memory database)
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    List<User> users = jdbi.withHandle(handle -> {
        handle.execute("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)");
        // Inline positional parameters
        handle.execute("INSERT INTO user(id, name) VALUES (?, ?)", 0, "Alice");
        // Positional parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (?, ?)").bind(0, // 0-based parameter indexes
        1).bind(1, "Bob").execute();
        // Named parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bind("id", 2).bind("name", "Clarice").execute();
        // Named parameters from bean properties
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bindBean(new User(3, "David")).execute();
        // Easy mapping to any type
        return handle.createQuery("SELECT * FROM user ORDER BY name").mapToBean(User.class).list();
    });
    assertThat(users).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::core[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 29 with Jdbi

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

the class IntroductionTest method sqlObject.

// end::sqlobject-declaration[]
@Test
public void sqlObject() {
    // tag::sqlobject-usage[]
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    jdbi.installPlugin(new SqlObjectPlugin());
    // Jdbi implements your interface based on annotations
    List<User> userNames = jdbi.withExtension(UserDao.class, dao -> {
        dao.createTable();
        dao.insertPositional(0, "Alice");
        dao.insertPositional(1, "Bob");
        dao.insertNamed(2, "Clarice");
        dao.insertBean(new User(3, "David"));
        return dao.listUsers();
    });
    assertThat(userNames).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::sqlobject-usage[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Test(org.junit.Test)

Example 30 with Jdbi

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

the class TestUrls method setUp.

@Before
public void setUp() throws Exception {
    Jdbi jdbi = Jdbi.create("jdbc:sqlite::memory:");
    jdbi.installPlugin(new SQLitePlugin());
    handle = jdbi.open();
    handle.useTransaction(handle -> handle.execute("CREATE TABLE foo(url URL);"));
}
Also used : SQLitePlugin(org.jdbi.v3.sqlite3.SQLitePlugin) Jdbi(org.jdbi.v3.core.Jdbi) Before(org.junit.Before)

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