1

I'm trying to explore the basics of Spring's jdbctemplate. I'm having trouble with a simple example:

public static void main(String[] args) throws Exception {
    JdbcDataSource dataSource = new JdbcDataSource();
    String url = "jdbc:h2:mem:test";
    dataSource.setURL(url);

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    jdbcTemplate.execute("create table something (id int primary key, name varchar(100))");
    jdbcTemplate.execute("insert into something (id, name) values (1, 'Brian')");
}

Triggers this:

Caused by: org.h2.jdbc.JdbcSQLException: Table "SOMETHING" not found; SQL statement:
insert into something (id, name) values (1, 'Brian') [42102-177]

If I convert that code to use the raw JDBC api, it works fine:

public static void main(String[] args) throws Exception {
    JdbcDataSource dataSource = new JdbcDataSource();
    String url = "jdbc:h2:mem:test";
    dataSource.setURL(url);

    try (Connection dbConnection = dataSource.getConnection()) {
        try (Statement statement = dbConnection.createStatement()) {
            statement.execute("create table something (id int primary key, name varchar(100))");
            statement.execute("insert into something (id, name) values (1, 'Brian')");

            try (ResultSet rs = statement.executeQuery("select id, name from something")) {
                while (rs.next()) {
                    System.out.println(String.format("got result. id=%d. name=%s", rs.getInt(1), rs.getString(2)));
                }
            }
        }
    }
}

If it matters, my Gradle dependencies:

    compile 'org.springframework:spring-jdbc:4.0.6.RELEASE'
    compile 'com.h2database:h2:1.4.177'
clay
  • 18,138
  • 28
  • 107
  • 192

1 Answers1

0

please try to use update method instead execute.

Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89
hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
  • I just tried that. I get the exact same error. To be clear, I changed "execute" to "update" on the "insert" command. FYI, in the raw JDBC version both execute and executeUpdate work. – clay Sep 04 '14 at 14:50