I have created a tool called 'localdb' specifically for handling such a 
case a few years back (and should still be compatible with current H2 
versions since no protocol changes were made for the filesystem jdbc 
access).

Link: https://code.google.com/p/localdb

The wiki page contains the how-to information.

There was a previous case in the h2-database Google Group in the year of 
2011 which you could refer at:
https://groups.google.com/forum/#!topic/h2-database/eoId0GtOgUo

On Monday, February 10, 2014 3:41:09 PM UTC, Alejandro Villamarin wrote:
>
> Hi everyone,
>
> I'm running into a very strange issue, but I'm not sure if it is because 
> of me or because I'm doing something wrong. Let me depict my scenario.
>
> I'm trying to use H2 to embed a database in a Java app. If the db does not 
> exist, should be newly created and saved to disk (file) once the app 
> finishes. 
>
> That said, and regarding the documentation:
>
> Creating New Databases 
>
> By default, if the database specified in the URL does not yet exist, a new 
> (empty) database is created automatically. The user that created the 
> database automatically becomes the administrator of this database. 
>
> Auto-creating new database can be disabled, see Opening a Database Only 
> if it Already 
> Exists<http://h2database.com/html/features.html#database_only_if_exists>. 
>
>
> First thing weird enough is that a new database is not created 
> automatically....at least not in the filesystem, maybe this is done 
> in-memory (???? not sure, is not specified in the docs). This leads me to 
> my other question, where does H2 stores the data once finished? I can't see 
> it anywhere in my filesystem.
>
>
> Here is my code that "creates" the db:
>
> try {
>             spath = "jdbc:h2:file:data/sample";
>             conn = DriverManager.getConnection(spath, "john", "doe");
>    
> } 
> catch (Exception e) {
>             System.out.println(e.getMessage());
> }
>
> If right after executing this I look at my relative path + /data....there 
> is nothing...funny thing is that if I execute this right after:
>
>     try {
>             Statement stat = conn.createStatement();
>              
>             //create table
>             stat.execute("CREATE TABLE ACTIVITY (ID INTEGER, STARTTIME 
> datetime, ENDTIME datetime,  ACTIVITY_NAME VARCHAR(200),  PRIMARY KEY 
> (ID))");
>     
>             //prepared statement
>             PreparedStatement prep = conn.prepareStatement("INSERT INTO 
> ACTIVITY (ID, STARTTIME, ENDTIME, ACTIVITY_NAME) VALUES (?,?,?,?)");
>     
>             //insert 10 row data
>             for (int i = 0; i<10; i++){
>                 prep.setLong(1, i);
>                 prep.setTimestamp(2, new 
> Timestamp(System.currentTimeMillis()));
>                 prep.setTimestamp(3, new 
> Timestamp(System.currentTimeMillis()));
>                 prep.setString(4, "Activity-" + i);
>     
>                 //batch insert
>                 prep.addBatch();
>             }
>             conn.setAutoCommit(false);
>             prep.executeBatch();
>             conn.setAutoCommit(true);
>     
>     
>     
>     
>             ResultSet rs = stat.executeQuery("Select STARTTIME, ENDTIME, 
> ACTIVITY_NAME from ACTIVITY");
>             while (rs.next()) {
>     
>                     Date start = rs.getTimestamp(1);
>                     Date end = rs.getTimestamp(2);
>                     String activityName = rs.getString(3);
>     
>                     //print query result to console
>                     System.out.println("activity: " + activityName);
>                     System.out.println("start: " + start);
>                     System.out.println("end: " + end);
>                     System.out.println("--------------------------");
>                 }
>                 rs.close();
>                 //close connection
>                 conn.close();
>         } 
>         catch (SQLException e) {
>             e.printStackTrace();
>         }
>
> I get this output:
>
>
> activity: Activity-0
> start: 2014-02-10 15:27:07.414
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-1
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-2
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-3
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-4
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-5
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-6
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-7
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-8
> start: 2014-02-10 15:27:07.418
> end: 2014-02-10 15:27:07.418
> --------------------------
> activity: Activity-9
> start: 2014-02-10 15:27:07.419
> end: 2014-02-10 15:27:07.419
> --------------------------
>
>
> Thus, the db seems to exist, but when the connection is closed, I check my 
> relative path again and there is nothing....I assume H2 is creating the 
> database in-memory, altough using file when creating the connection.
>
> Thus, how can I "dump" the in-memory database to file once finished?
>
> Thanks!
>
> Alejandro
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to h2-database+unsubscr...@googlegroups.com.
To post to this group, send email to h2-database@googlegroups.com.
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to