'How to store H2 database file into project directory

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?



Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fiskra
Solution 2 Vikas Mahato