Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does H2's Embedded Databases Store the data?

So I just recently started learning about how databases work, how to use SQL ect. and decided to start implementing an embedded database into my Java application (specifically the H2 database) and seemed to work fairly well on the computer I was coding on.

When I moved over to a different computer to continue my coding I noticed that even if I ported the embedded database file (h2-*.jar) All of the prepared tables I created in the first computer don't exist on the second one. I somehow had the preconception that the actual data generated through the database engine are also stored in the embedded database file.

So my question is, where is the data from the database actually stored? is it possible to prepare a database which already contains thousands of records and distribute it with the actual application?

I should also mention that the way I connect to the database on the first computer was through a JDBC connection i.e. the URL: JDBC:h2:~/test and when I tried to connect to that database on the second computer it did not exist.

Thanks!

like image 980
Sammy Guergachi Avatar asked Feb 27 '12 08:02

Sammy Guergachi


People also ask

How do you persist data in H2 DB?

Persist the data in H2 Database If we want to persist the data in the H2 database, we should store data in a file. To achieve the same, we need to change the datasource URL property. In the above property, the sampledata is a file name.

How do I access H2 DB files?

Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .

What is H2 embedded database?

H2 is a relational database management system written in Java. It can be embedded in Java applications or run in client-server mode. H2 Database Engine. Initial release.

How does H2 database work?

H2 is an open-source lightweight Java database. It can be embedded in Java applications or run in the client-server mode. H2 database can be configured to run as in-memory database, which means that data will not persist on the disk.


1 Answers

Read the FAQ:

Where are the Database Files Stored?

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\<userName> or C:\Users\<userName>. 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 <Installation Directory>/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

like image 86
The Nail Avatar answered Oct 09 '22 04:10

The Nail