Search in sources :

Example 21 with Update

use of org.jdbi.v3.core.statement.Update 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());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Update(org.jdbi.v3.core.statement.Update) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 Handle (org.jdbi.v3.core.Handle)10 Update (org.jdbi.v3.core.statement.Update)8 Something (org.jdbi.v3.core.Something)6 Jdbi (org.jdbi.v3.core.Jdbi)4 ExtensionMethod (org.jdbi.v3.core.extension.ExtensionMethod)4 HashMap (java.util.HashMap)3 SqlUpdate (org.jdbi.v3.sqlobject.statement.SqlUpdate)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1