Spring H2 Database, Not Creating table in Database -


the following code creating test.mv.db file. run , show has inserted data in database.

schema:

create table customer (     id identity,     firstname varchar(20) not null,     lastname varchar(20) not null ); 

main class:

public class app {      public static void main(string[] args) {          annotationconfigapplicationcontext ctx = new annotationconfigapplicationcontext(config.class);          customerrepository repo = ctx.getbean(customerrepository.class);          customer c1 = new customer("john", "doe");         customer c2 = new customer("jane", "doe");          // insert         c1 = repo.save(c1);         c2 = repo.save(c2);          for(customer t : repo.findall()) {             system.out.println(t.getfirstname()+", "+t.getlastname());         }         ctx.close();     }  } 

config:

@configuration @componentscan public class config {      @bean     public datasource datasource() {         drivermanagerdatasource ds = new drivermanagerdatasource();          ds.setdriverclassname("org.h2.driver");         ds.seturl("jdbc:h2:~/test;db_close_delay=-1;db_close_on_exit=false");          resource schema = new classpathresource("schema.sql");         resourcedatabasepopulator dbp = new resourcedatabasepopulator();         dbp.addscript(schema);         databasepopulatorutils.execute(dbp, ds);          return ds;     }      @bean     public jdbcoperations jdbctemplate(datasource ds) {         return new jdbctemplate(ds);             }  } 

customer class:

public class customer {      private long id;     private string firstname;     private string lastname;      public customer() {}      public customer(long id, string firstname, string lastname) {         super();         this.id = id;         this.firstname = firstname;         this.lastname = lastname;     }      public customer(string firstname, string lastname) {         super();         this.firstname = firstname;         this.lastname = lastname;     }      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      public string getfirstname() {         return firstname;     }      public void setfirstname(string firstname) {         this.firstname = firstname;     }      public string getlastname() {         return lastname;     }      public void setlastname(string lastname) {         this.lastname = lastname;     }    } 

customerrepository interface:

public interface customerrepository {      customer findone(long id);      customer save(customer cust);      list<customer> findall();      int update(customer cust);      int delete (customer cust);  } 

customerrepositoryimpl:

@repository public class customerrepositoryimpl implements customerrepository {      @autowired     private jdbcoperations jdbc;      private static final string sql_insert = "insert customer (firstname, lastname) values (?, ?)";     private static final string sql_update = "update customer set firstname=?, lastname=? id=?";     private static final string sql_find_one = "select * customer id = ?";     private static final string sql_find_all = "select * customer order lastname";     private static final string sql_delete_one = "delete customer id = ?";       public customer findone(long id) {         return jdbc.queryforobject(sql_find_one, new customerrowmapper(), id);     }      public customer save(final customer cust) {          keyholder holder = new generatedkeyholder();          int rows = jdbc.update(new preparedstatementcreator() {              public preparedstatement createpreparedstatement(connection conn) throws sqlexception {                 preparedstatement ps = conn.preparestatement(sql_insert, new string[]{"id"});                  ps.setstring(1,  cust.getfirstname());                 ps.setstring(2, cust.getlastname());                  return ps;             }         }, holder);          if(rows == 1) { // success, apply id customer object             cust.setid((long)holder.getkey());             return cust;         }          return null;      }      public list<customer> findall() {         return jdbc.query(sql_find_all, new customerrowmapper());     }      public int update(customer cust) {         return jdbc.update(sql_update, cust.getfirstname(), cust.getlastname(), cust.getid());     }      public int delete(customer cust) {         return jdbc.update(sql_delete_one, cust.getid());     }      private class customerrowmapper implements rowmapper<customer> {          public customer maprow(resultset rs, int row) throws sqlexception {              return new customer(rs.getlong("id"), rs.getstring("firstname"), rs.getstring("lastname"));           }      } } 

stacktrace:

aug 14, 2017 9:45:42 pm org.springframework.context.annotation.annotationconfigapplicationcontext preparerefresh info: refreshing org.springframework.context.annotation.annotationconfigapplicationcontext@b81eda8: startup date [mon aug 14 21:45:42 bdt 2017]; root of context hierarchy aug 14, 2017 9:45:43 pm org.springframework.jdbc.datasource.drivermanagerdatasource setdriverclassname info: loaded jdbc driver: org.h2.driver aug 14, 2017 9:45:43 pm org.springframework.jdbc.datasource.init.scriptutils executesqlscript info: executing sql script class path resource [schema.sql] aug 14, 2017 9:45:43 pm org.springframework.jdbc.datasource.init.scriptutils executesqlscript info: executed sql script class path resource [schema.sql] in 28 ms. aug 14, 2017 9:45:43 pm org.springframework.context.annotation.annotationconfigapplicationcontext doclose info: closing org.springframework.context.annotation.annotationconfigapplicationcontext@b81eda8: startup date [mon aug 14 21:45:42 bdt 2017]; root of context hierarchy john, doe jane, doe 

problem

but on second run saved data no more there. first checked dbeaver, if there table within database named customer. not find any.

then commented out following lines -

c1 = repo.save(c1); c2 = repo.save(c2); 

from app.java

becasue, if there data read there

for(customer t : repo.findall()) {     system.out.println(t.getfirstname()+", "+t.getlastname()); } 

but no luck here well.

what can problem please?

a working solution derby welcome database saved in pc.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -