简体   繁体   中英

(Spring Boot and H2) Can't use h2 database

I'm learning Spring boot and I'm trying to create a very simple RESTful API that access an in-memory database and perform CRUD actions.

However, everytime I try to Connect or Test Connection on http://localhost:8080/h2-console , I get this error:

"Database "C:/Users/XXX/test" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146"

https://imgur.com/a/oYgkK1C

I followed EXACTLY the instructions from http://www.springboottutorial.com/spring-boot-crud-rest-service-with-jpa-hibernate . I have tried everything I could find online: using jdbc:h2:mem:test as JDBC URL etc, but none of them worked for me and I'm not sure what am I doing wrong. I didn't install h2 database from the official website as I read it is not necessary to use the in-memory module (and I still don't know if I should've installed it or not, as there is not a single mention of it online, as far as I checked).

Any thoughts? I'm a beginner when it comes to Spring Boot and I'm really lost. I just want to test CRUD actions and I don't care about the persistence of the DB.

I have provided my application.properties below.

Thank you: :)

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

# Datasource
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=user
spring.datasource.password=user
spring.datasource.driver-class-name=org.h2.Driver

Make sure your url in h2-console(refer screenshot below) is same as your ' spring.datasource.url=jdbc:h2:mem:test '.

It worked for me.

在此处输入图像描述

UPDATE : Other alternative solution is, you can avoid setting spring.datasource.url property. Spring will automatically set this default JDBC url for you.

happy learning.. Upvote, if it is sovled your issue.

In your spring application.properties file, set the property spring.datasource.url=jdbc:h2:~/test

Then restart the application and open http://localhost:8080/h2-console/ Click on Test Connection button, you should see "Test successful".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM