Search code examples
spring-dataspring-jdbcjdbctemplate

Spring store data in jdbcTemlate(h2 db) permanently


I am starting to learn Spring and faced with some issues regarding spring-jdbc. First, I tried run the example from this: https://spring.io/guides/gs/relational-data-access/ and it worked. Then, I commented lines with droping and creating new tables(http://pastebin.com/zcJHsL1P), in order to not override data, but just get it from db and show it. However, spring showed me error:

Table "CUSTOMERS" not found; SQL statement: ...

So, my question is: What should I do to store my database permanently? I don't want to recreate all time new database, I want create it once and update it.

P.S. I used H2 database. Maybe problem exists in tis db?


Solution

  • That piece of code looks like you are "prototyping" something; so it's easier to automatically create a new database (schema, tables, data) on the fly, execute and/or test whatever you want to...and finish the execution.

    If you want to persist your data and only modify/update it, either use H2 with the "file layout" or use MySQL, PostreSQL, etcetera.

    By the way, the reason you are getting Table "CUSTOMERS" not found; SQL statement: ... is because you are using H2 as an in-memory database and every time you start your application you need to re-create the tables and populate them with data.