How do I create a database on HSQLDB?

Asked

Viewed 404 times

1

Usually to create a database is through the command CREATE database and the name of the bank.

However, I need to help a friend of mine for a college job, and I can’t figure out how to create a database in this database.

I want to create a new database to make the connection in Eclipse. How to create a database with HSQLDB?

1 answer

1


According to the documentation:

When a server instance is Started, or when a Connection is made to an in-process database, a new, Empty database is created if no database exists at the Given path.

I mean, there is no create database ... to create a database, simply instantiate a connection:

Connection c = DriverManager.getConnection("jdbc:hsqldb:file:/opt/db/testdb", "SA", "");

The above method creates the empty database file, this is the default for HSQLDB. But care must be taken that if the path is misinformed, it will not fail, because a new bank will be created in the wrong way. To avoid this, it is interesting to add the argument ;ifexists=true on the connection after the bank already exists, to avoid this kind of problem.

Connection c = DriverManager.getConnection(
     "jdbc:hsqldb:file:/opt/db/testdb;ifexists=true", "SA", "");

That way, if the path is changed and the bank does not exist on that path, it will fire an exception.

The shapes shown are for bank connection standalone, to the server mode there are other ways:

  • local bank connection on the same machine using protocol hsql:

    Connection c = DriverManager.getConnection(
         "jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
    
  • using the HTTP protocol:

    Connection c = DriverManager.getConnection(
         "jdbc:hsqldb:http://localhost/xdb", "SA", "");
    

Where xdb is the name of the database on which it will be connected.

Note: "SA" is the default HSQLDB user without password, so the above codes were presented with it. If you want to create your own user and password, these should be informed when creating the bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.