use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.
the class TestVendorArrays method testHsqlDb.
@Test
public void testHsqlDb() {
Jdbi db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID());
init(db);
try (Handle handle = db.open()) {
handle.execute("create table player_stats (" + "name varchar(64) primary key, " + "seasons varchar(36) array, " + "points int array)");
handle.createUpdate("insert into player_stats (name,seasons,points) values (?,?,?)").bind(0, "Jack Johnson").bind(1, new String[] { "2013-2014", "2014-2015", "2015-2016" }).bind(2, new Integer[] { 42, 51, 50 }).execute();
String[] seasons = handle.createQuery("select seasons from player_stats where name=:name").bind("name", "Jack Johnson").mapTo(String[].class).findOnly();
assertThat(seasons).containsExactly("2013-2014", "2014-2015", "2015-2016");
}
}
use of org.jdbi.v3.sqlobject.customizer.Bind 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.sqlobject.customizer.Bind in project jdbi by jdbi.
the class TestArgumentFactory method testRegisterOnHandle.
@Test
public void testRegisterOnHandle() throws Exception {
try (Handle h = dbRule.openHandle()) {
h.registerArgument(new NameAF());
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.sqlobject.customizer.Bind 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.sqlobject.customizer.Bind in project jdbi by jdbi.
the class TestClasspathSqlLocator method testUsefulExceptionForBackTracing.
@Test
public void testUsefulExceptionForBackTracing() throws Exception {
Handle h = dbRule.openHandle();
exception.expect(StatementException.class);
exception.expectMessage("insert into something(id, name) values (:id, :name)");
exception.expectMessage("insert into something(id, name) values (?, ?)");
h.createUpdate(findSqlOnClasspath("insert-id-name")).bind("id", 1).execute();
}
Aggregations