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");
}
}
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[]
}
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[]
}
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[]
}
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);"));
}
Aggregations