Getting Started with the HSQL Database Engine: A Beginner’s Guide
What HSQLDB is
HSQLDB (HyperSQL Database) is a lightweight, open-source relational database written in Java. It supports in-memory and disk-based tables, full SQL-92/SQL:2008 features, transactions, stored procedures, and JDBC connectivity, making it suitable for embedded applications, testing, and small-to-medium production systems.
Quick use cases
- Embedded desktop or desktop-app storage
- Unit and integration testing (fast in-memory mode)
- Lightweight server for small services
- Education and prototyping
Editions and modes
- In-memory mode: Fast, ephemeral, data lost on shutdown unless persisted.
- Server mode (TCP/HTTP): Multi-client access like a typical RDBMS.
- Disk-based (file) mode: Persistent storage using .script/.data files or cached tables.
Installation
- Download the HSQLDB distribution JAR from the official site or Maven Central.
- Add the JAR to your project’s classpath or as a Maven/Gradle dependency:
- Maven:
Code
org.hsqldb hsqldb 2.7.2
- Maven:
- Optionally unpack the distribution to use the provided server and client scripts.
Starting a simple server (quick example)
Run the server from command line:
Code
java -cp /path/to/hsqldb.jar org.hsqldb.server.Server –database.0 file:mydb –dbname.0 xdb
Connect with JDBC URL:
Code
jdbc:hsqldb:hsql://localhost/xdb
Or use in-memory mode in code:
java
String url = “jdbc:hsqldb:mem:mydb”; Connection c = DriverManager.getConnection(url, “SA”, ””);
Basic SQL examples
- Create table:
sql
CREATE TABLE person ( id INTEGER IDENTITY PRIMARY KEY, name VARCHAR(100), created TIMESTAMP DEFAULT CURRENTTIMESTAMP );
- Insert:
sql
INSERT INTO person (name) VALUES (‘Alice’);
- Query:
sql
SELECT id, name, created FROM person;
JDBC tips
- Default user is SA with empty password.
- Use org.hsqldb.jdbc.JDBCDriver (auto-registered in modern JDBC).
- Remember to commit when using transactional modes:
java
conn.setAutoCommit(false); ... // statements conn.commit();
Persistence and shutdown
- For file-based databases, call SHUTDOWN to ensure changes are flushed:
sql
SHUTDOWN;
- In-memory databases lose data unless you use SCRIPT or regularly persist to file.
Common pitfalls
- Forgetting to SHUTDOWN file databases can cause recovery on next start.
- Using in-memory mode for production without persistence planning.
- Relying on HSQLDB clustering—it’s not designed for heavy distributed workloads.
Resources to learn more
- Official HSQLDB User Guide (search for the latest version)
- JDBC tutorials and examples
- Community forums and GitHub repository for issues and contributions
Leave a Reply
You must be logged in to post a comment.