How to store H2 database file into project directory
Asked Answered
W

2

13

When I used H2 database the database files are stored at C:\Users\MyName\TestDataBase.db directory. The H2 path is jdbc:h2:~/TestDataBase. This is default H2 database path.

Is there a possibility to store H2 database file into my project directory like this C:\Users\MyName\EclipseWorkspace\ProjectName\TestDataBase.db and how to config path to do this?

Wildfire answered 18/4, 2017 at 10:58 Comment(1)
Possible duplicate of Where does H2's Embedded Databases Store the data?Paquito
K
27

If you have h2 console you can basically create a new db wherever you want. If you read carefully h2 faq, they described very well how you can do that.

When using database URLs like jdbc:h2:~/test, the database is stored in the user directory. For Windows, this is usually C:\Documents and Settings\ or C:\Users\. If the base directory is not set (as in jdbc:h2:./test), the database files are stored in the directory where the application is started (the current working directory). When using the H2 Console application from the start menu, this is /bin. The base directory can be set in the database URL. A fixed or relative path can be used. When using the URL jdbc:h2:file:./data/sample, the database is stored in the directory data (relative to the current working directory). The directory is created automatically if it does not yet exist. It is also possible to use the fully qualified directory name (and for Windows, drive name). Example: jdbc:h2:file:C:/data/test

h2 faq

So just set your jdbc url to : jdbc:h2:file:C:/Users/MyName/EclipseWorkspace/ProjectName/TestDataBase

Kinesiology answered 18/4, 2017 at 11:44 Comment(1)
in UNIX like systems it start with ~/ jdbc:h2:file:~/MyName/EclipseWorkspace/ProjectName/TestDataBaseLeid
L
5

If you want to create the database in the project itself, then I think this should work:

spring.datasource.url=jdbc:h2:file:./TestDataBase

Notice the ./ before the database name.

Larry answered 23/4, 2022 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.