How to configure spring-boot to use file based H2 database

How to configure spring-boot to use file based H2 database

To configure a Spring Boot application to use a file-based H2 database, you need to specify the database URL and configure a few properties in the application.properties or application.yml file. Here are the steps to do it:

  1. Add H2 Database Dependency: Make sure you have the H2 database dependency in your pom.xml file (if using Maven) or build.gradle (if using Gradle):

    For Maven:

    <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> 

    For Gradle:

    dependencies { runtimeOnly 'com.h2database:h2' } 
  2. Configure Database Properties: In your application.properties or application.yml file, configure the database properties. For an H2 file-based database, you should set the spring.datasource.url property to specify the database file path:

    For application.properties:

    spring.datasource.url=jdbc:h2:file:/path/to/your/database spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=your-username spring.datasource.password=your-password 

    For application.yml:

    spring: datasource: url: jdbc:h2:file:/path/to/your/database driverClassName: org.h2.Driver username: your-username password: your-password 

    Replace /path/to/your/database with the desired path to your H2 database file. You can also specify a relative path if you want the database file to be created in the project's working directory.

  3. Enable H2 Console (Optional): If you want to use the H2 Console to access the database web interface, you can enable it in your application.properties or application.yml file:

    For application.properties:

    spring.h2.console.enabled=true spring.h2.console.path=/h2-console 

    For application.yml:

    spring: h2: console: enabled: true path: /h2-console 

    This will make the H2 Console available at http://localhost:8080/h2-console (assuming your application runs on port 8080). You can adjust the path and other properties as needed.

  4. Run Your Spring Boot Application: Start your Spring Boot application. Spring Boot will automatically configure and use the H2 database with the specified file path.

  5. Access H2 Console (Optional): If you enabled the H2 Console, you can access it by navigating to the URL specified in the spring.h2.console.path property (e.g., http://localhost:8080/h2-console). You can use this web interface to interact with your H2 database.

Remember that this setup is for development or testing purposes. In a production environment, you would typically use a more robust database like MySQL, PostgreSQL, or others.


More Tags

jitpack spring-cloud-gateway delete-file yahoo eclipse-classpath gettype wysiwyg tablet azure windows-server-2008

More Java Questions

More Various Measurements Units Calculators

More Cat Calculators

More Transportation Calculators

More Physical chemistry Calculators