How to store H2 database file into project directory

27,884

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

Share:
27,884
Armer B.
Author by

Armer B.

Java Developer

Updated on August 05, 2022

Comments

  • Armer B.
    Armer B. almost 2 years

    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?

  • acakojic
    acakojic about 2 years
    in UNIX like systems it start with ~/ jdbc:h2:file:~/MyName/EclipseWorkspace/ProjectName/TestDataB‌​ase