SQL Tutorial with HSQLDB

This assumes that you have Java setup Java on your machine as illustrated in the “setting up” Java tutorial.

Q. Why use HSQLB?
A. It can be useful in a number of ways.

  • Easy to get started and if you are a beginner, you can acquire or sharpen your SQL and database skills.
  • It is open-sourced, hence you can use it for your self-taught projects as a database. An alternative is MySQL database.
  • It is used in commercial projects to write unit tests for your data access layer. Apache Derby database is another alternative.
  • It is used in the ensuing tutorials on JDBC and Hibernate as the database.

Note: If you are serious about expanding this into a proper self-taught project “MySQL” is a better choice.

Step 1: Download the latest version of HSQLB from  http://hsqldb.org/. I downloaded hsqldb-2.2.9.zip at the time of writing and extracted zip files. The HSQLDB is unpacked under c:\Tools as as hown below.


Step 2: You can start the server with the following command.

Step 3: Open up another command prompt and start the DatabaseManager, with which you can execute SQLs. If you are new to SQL, then this is handy to practice and gain some SQL skills. Note: You need to have the HSQLDB server running before you can open the DatabaseManager.


Once you press “Ok”, the next window will look like


Step 4: Here you can type and execute some DDL(i.e. Data Definition Language) to create new table schema, etc and DML (i.e. Data Manipulation Language) to insert new data, etc. Type in the following commands  as grouped and press the “Execute” button.

a. DDL to create a table named “Course

b. DML to insert a few records

c. DML to select the records we just had created

You can now see the in memory table and the data by selecting the menu options View –> Refresh Tree.


Note: The syntax for these files are .properties, .lck etc. Since we have not specified any database name, it defaults to “test”.

This was a quick overview. Now, let’s look at creating a named database.

HSQLDB supports three types of persistent tables, MEMORY tables, CACHED tables and TEXT tables.I will be using the default MEMORY tables where data is held entirely in memory but any change to their structure or contents is written to the .script file. The script file is read the next time the database is opened, and the MEMORY tables are recreated with all their contents. So, the MEMORY tables are persistent. It is important to remember that the data in memory is written to the .script file when you shutdown your database properly (i.e. naturally by executing SQL “SHUTDOWN  (COMPACT | IMMEDIATELY”), the saved file will load the data into memory the next time the SQLDB server starts up. But if you stop the HSQLDB server abruptly in the command line by pressing [Ctrl] + [C] the data will not be written to the script file and consequently lost. Refer documentation for CACHED & TEXT tables.

Step 1: Cretate a file named server.properties under c:\Tools\hsqldb-2.2.9\hsqldb and type in the following database file name (i.e. tutorialdb) and the database alias name (mytestdb)

You can now start the database server with the following command


Once the server is started, you an see the following folder and files being created.


Step 2: Once the server has started, start the DatabaseManager where you can manage your schemas and data.

c:\Tools\hsqldb-2.2.9\hsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.util.DatabaseManager


Note that the protocol is “hsql” and not “mem“.

You can type in the relevant DDL and DML SQL queries as you did earlier. This is still an in-memory database and the data will be lost in between invocations.

Note: To persist your data from memory, you need to execute the “SHUTDOWN” SQL command, which will delete the tutorialdb.lock and and tutorialdb.log files and if you open the tutorialdb.script file, you can see your SQL commands that get executed when you reconnect again.

If you want to persist it directly to a file without having an alias, you can try.


Once you know how HSQLDB works, you can work on JDBC and Hibernate tutorials.

Java Interview FAQs

800+ Java Interview Q&As