I have created a spring boot application to connect h2 database with it. While doing so, it throws an error showing Database not found. Please help me with the solution which I can implement and resolve the issue.
I have added com.h2database dependency in the pom.xml file, then also it gives the error.
Below is my pom.xml file and application.properties file
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>santanderdbproj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstproject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
application.properties
spring.datasource.url = jdbc:h2:file:C:/data/sample
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2
This is an incorrect error message that appears only in 1.4.198 and 1.4.199. The next version of H2 (1.4.200) will show a better message like
Database … not found, either pre-create it or allow remote database creation (not recommended in secure environments)
You see this error message because your database doesn't exist yet. The normal way to fix your problem is to create the database first with your application before trying to login into it with H2 Console.
H2 Console can be configured to allow database creation, but it may create a security hole in your system, anyone who can open this page may do anything with your system with yours access permissions in such configuration.
Note that H2 Console in browser's session launched by H2 from its icon in the system tray (you can simply launch the h2-1.4.199 jar as Java application, or use java -jar h2-1.4.199.jar
) gives you permission to create a new database in secure way. You can use it for that purpose. If you're not planning to use it as a TCP Server, close it from the system tray icon after database creation to make sure that it doesn't hold your database. You can also use the command-line Shell tool:
https://h2database.com/html/tutorial.html#creating_new_databases
H2 Console from the Spring doesn't ship with such feature.
Older versions of H2 (up to 1.4.197) also allow the database creation, including the console from the Spring, but, again, it creates a security hole in you system.