use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.
the class BindListTest method init.
@BeforeClass
public static void init() {
final Jdbi db = dbRule.getJdbi();
db.installPlugin(new SqlObjectPlugin());
db.registerRowMapper(new SomethingMapper());
handle = db.open();
handle.execute("insert into something(id, name) values(1, '1')");
handle.execute("insert into something(id, name) values(2, '2')");
// "control group" element that should *not* be returned by the queries
handle.execute("insert into something(id, name) values(3, '3')");
expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2"));
}
use of org.jdbi.v3.core.Jdbi 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();
}
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenTableCreated_thenInsertIsPossible.
@Test
public void whenTableCreated_thenInsertIsPossible() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
int updateCount = handle.execute("create table PROJECT_1 (id integer identity, name varchar(50), url varchar(100))");
assertEquals(0, updateCount);
updateCount = handle.execute("INSERT INTO PROJECT_1 VALUES (1, 'tutorials', 'github.com/eugenp/tutorials')");
assertEquals(1, updateCount);
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenMultipleParameters_thenReplacement.
@Test
public void whenMultipleParameters_thenReplacement() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
Update update = handle.createUpdate("INSERT INTO PROJECT_11 (NAME, URL) VALUES (:name, :url)");
Map<String, String> params = new HashMap<>();
params.put("name", "REST with Spring");
params.put("url", "github.com/eugenp/REST-With-Spring");
update.bindMap(params);
int rows = update.execute();
assertEquals(1, rows);
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
assertEquals(1, list.size());
class Params {
private String name;
private String url;
public Params(String name, String url) {
this.name = name;
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
update.bindBean(new Params("tutorials", "github.com/eugenp/tutorials"));
rows = update.execute();
assertEquals(1, rows);
list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
assertEquals(2, list.size());
});
}
use of org.jdbi.v3.core.Jdbi 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));
});
}
Aggregations