How do I create a database in HSQLDB?

2

Generally, to create a database is through the command CREATE database and the name of the database.

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

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

    
asked by anonymous 20.11.2017 / 10:28

1 answer

1

According to 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 the database exists at the given path.

That is, there is no create database ... to create a database, just 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 one must be careful because if the path is erroneously informed, it will not give error, because a new bank will be created in the wrong way. To avoid this, it is interesting to add the argument ;ifexists=true to the connection after the database already exists, to avoid this type of problem.

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

In this way, if the path is changed and the bank does not exist in that path, it throws an exception.

The forms shown are for bank connection standalone >, for server mode other ways : / p>

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

    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 you want to connect to.

  

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

    
20.11.2017 / 12:11