Create JDBC Table

My web application written in Spring Web-MVC uses JDBC to work with data. I want my application to automatically create tables (and a schema) when the end user starts it the first time (but loads the created schema when it starts it again). I use HSQLDB as a database engine.

Any ideas how to do this? (I don’t want to write inside the application’s special methods to check if the table exists, and if not, create them. Is there any more useful method?)

PS I'm thinking of using Hibernate instead of a simple connection method. Is there any way to solve this with Hibernate?

+3
source share
3 answers

I do not want to write special application methods to check if a table exists, and if not, create them. Is there any other useful method?

Just create the DB with the installer of your web application. This can also be done on websites.

An alternative is an implementation ServletContextListenerthat checks the database and creates tables if necessary. ServletContextListenerruns only once during application launch, so you don’t need to check it with every request.

+3
source

Spring JDBC has a standard mechanism for initializing databases .

, DataSource bean, initialize-database spring -jdbc:

<jdbc:initialize-database data-source="dataSource">`
  <jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
  <jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>

:

<jdbc:initialize-database data-source="dataSource"
    enabled="#{systemProperties.INITIALIZE_DATABASE}">
  <jdbc:script location="..."/>
</jdbc:initialize-database>

: hibernate , :

+2

spring. JPA + Hibernate . , , , .. , . . SQL , , .. , .

+1

Source: https://habr.com/ru/post/1777228/


All Articles