use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenSelectMapToString_thenStream.
@Test
public void whenSelectMapToString_thenStream() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
Query query = handle.createQuery("select name, url from PROJECT_5 order by id");
query.mapTo(String.class).useStream((Stream<String> stream) -> assertEquals("tutorials", stream.findFirst().get()));
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenJdbiWithProperties_thenSuccess.
@Test
public void whenJdbiWithProperties_thenSuccess() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.open().close();
Properties properties = new Properties();
properties.setProperty("username", "sa");
properties.setProperty("password", "");
jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", properties);
jdbi.open().close();
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenTransactionRollback_thenNoDataInserted.
@Test
public void whenTransactionRollback_thenNoDataInserted() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.useTransaction(h -> {
handle.execute("create table PROJECT_12 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
handle.rollback();
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_12").mapToMap().list();
assertEquals(0, list.size());
});
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenParameters_thenReplacement.
@Test
public void whenParameters_thenReplacement() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
Update update1 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (?, ?)");
update1.bind(0, "tutorials");
update1.bind(1, "github.com/eugenp/tutorials");
int rows = update1.execute();
assertEquals(1, rows);
Update update2 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (:name, :url)");
update2.bind("name", "REST with Spring");
update2.bind("url", "github.com/eugenp/REST-With-Spring");
rows = update2.execute();
assertEquals(1, rows);
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'tutorials'").mapToMap().list();
assertEquals(1, list.size());
list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'REST with Spring'").mapToMap().list();
assertEquals(1, list.size());
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenSelectMapToMap_thenResultsAreMapEntries.
@Test
public void whenSelectMapToMap_thenResultsAreMapEntries() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
Query query = handle.createQuery("select * from PROJECT_3 order by id");
List<Map<String, Object>> list = query.mapToMap().list();
assertEquals("tutorials", list.get(0).get("name"));
assertEquals("REST with Spring", list.get(1).get("name"));
});
}
Aggregations