Database not found, and IFEXISTS=true, so we cant auto-create it

后端 未结 17 2902
不思量自难忘°
不思量自难忘° 2020-12-15 06:20

I am getting error after opening the h2 database console. I enter database name but it is showing database not found error:

Database \"C:/Users/Barlek

相关标签:
17条回答
  • 2020-12-15 06:57

    I have faced this issue and resolved in the following way

    • To use H2 Database - Your application should be running in JDK Environment, not JRE Environment , to change please use below steps :

      1. Right-click your project > properties
      2. Select “Java Build Path” on left, then “JRE System Library”, click Edit…
      3. Select "Workspace Default JRE"
      4. Click "Installed JREs"
      5. If you see JRE you want in the list select it (selecting a JDK is OK too)
      6. If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK
      7. Now you should see all installed JREs, select the one you want
      8. Click OK/Finish a million times. I have used Georgie provided steps here -> https://stackoverflow.com/a/29640138


    • In H2 Login console, default URL come up by Spring Boot will be jdbc:h2:~/test, it should actually match with the spring boot application properties spring.datasource.url=jdbc:h2:mem:testdb so replace the url in the login console and click on connect I have used [Ivan Xue][2] provided steps here -> https://stackoverflow.com/a/56539107

    • Please clear target using mvn clean command and allow project to build if doesn't enforce using maven force update snapshot or mvn install and then start your spring boot app again to see the difference
    0 讨论(0)
  • 2020-12-15 06:58

    Just append this to the application.properties file:

    spring.datasource.url=jdbc:h2:mem:testdb
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.username=sa
    spring.datasource.password=
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
    
    0 讨论(0)
  • 2020-12-15 06:58

    I found that you can add a parameter to the "-ifNotExists" start options when booting up h2 db. Programmatically, you can do it this way:

    Server.createTcpServer("-tcpPort", "[PORT]", "-tcpAllowOthers", "-ifNotExists").start();

    If you are using the command prompt, the same applies

    0 讨论(0)
  • 2020-12-15 06:59

    Use a pre-2019 version of the H2 database dependency that auto-creates the database every time you run your standalone application. For example version 1.4.193. Your pom.xml should include this dependency:

    <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.193</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-15 07:02

    Check The jdbc Url used in h2 console to see if it match what you have specified

    0 讨论(0)
  • 2020-12-15 07:03
    Along with h2 dependency in POM :
    <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
        </dependency>
    

    I added jpa, crud repository dependency in POM Like given below and It worked fine for me:

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
    0 讨论(0)
提交回复
热议问题