2

It is very easy and well documentented how to run H2 in server mode. Just code:

Server.createTcpServer().start();

Very easy. But I'm unable to find an answer on:

  • how to give the created database an other name than 'test', because this is the default name.
  • how to persist the data. As it seems, this way the server starts an in-memory DB. But what I'm looking for is something that persists the data permanently.

Any idea?

honk
  • 9,137
  • 11
  • 75
  • 83
  • Maybe [this](http://stackoverflow.com/q/8068843/2675154) post on SO helps at least partially... – honk Sep 18 '14 at 12:40

2 Answers2

3

As described in Connecting to a Database using JDBC, the database file name may be specified in the database URL, for example

jdbc:h2:tcp://localhost/~/src/java/MyDatabase;IFEXISTS=TRUE

Data will be stored in the file MyDatabase,h2.db.

See Opening a Database Only if it Already Exists for the effect of specifying ;IFEXISTS=TRUE. Compare the URL with these embedded mode examples.

Addendum: Where will you give this URL?

Once the server is started, you can connect to the running database by passing the URL to your preferred client, as suggested here, or programmatically via java.sql.Connection, as shown here.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I read this many times. But where will you give this URL in. Take a look into the API I only find this properties Supported options are: -tcpPort, -tcpSSL, -tcpPassword, -tcpAllowOthers, -tcpDaemon, -trace, -ifExists, -baseDir, -key. See the main method for details. Additionally I can see the in main that '-properties ' are possible, but I'm unable to find any description of properties. And from the API Server has only a method getURL, not a setURL. I don't know what to do. – Andreas Riedel Sep 19 '14 at 05:33
  • @AndreasRiedel: The easiest way is to browse `http://localhost:8082`, as suggested [here](http://stackoverflow.com/a/2761825/230513); more above. – trashgod Sep 19 '14 at 07:39
2

After an other day and switch to derby I find a solution for this problem. I don't know why this is not handled by the H2 documention, but ok.
First at all. For me the connection url and the physical location of the DB-Files are different things. And I don't like this file things, because I want to take a look in the db on the fly.

So, whats to is this

private Server startDb() {
    Server retVal = null;

    try {
        final String userDir = System.getProperty("user.dir");
        // System.setProperty("h2.baseDir", userDir + "/data/jumpstart");

        retVal = Server.createTcpServer("-baseDir", userDir + "/data/jumpstart");
        retVal.start();

        Connection conn = null;
        try {
            Class.forName("org.h2.Driver");
            conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/jumpstart", "sa", "sa");
        } finally {
            if (conn != null)
                conn.close();
        }
    } catch (final Exception ex) {

    }

    return retVal;
}

As you can see here, the files of the db will be stored in the directoy /data/jumpstart.

The URL of JDBC connect hide this detail. It only address the the DB with name jumpstart.

That it is.

HTH
Andreas