hsqldb.org Home Page hsqldb - 100% Java Database

Lightweight 100% Java SQL Database Engine

How To / FAQ


How to install and run HSQLDB
How to compile HSQLDB
How to create a new database
How to start programming JDBC / HSQLDB
Where to get more documentation
How to use HSQLDB inside JBuilder
Selecting big results / Scanning big tables
Java Stored Procedures / Functions
How to upgrade from an old version


Reliability, Performance and Deployment

Documentation



How to install and run HSQLDB


How to compile HSQLDB

A compiled JAR of HSQLDB is included in the download. This runs under JRE 1.4 or later. If you want to re-compile for other JDK's, you will need a JDK and ANT. See the documentation in the /build directory.

Command line compiler tools are included in the free JDK (Java Development Kit) from Sun.


How to create a new database

A new database is created automatically if it does not yet exist. Just connect to the not-yet-existing database using the jdbc:hsqldb:file:«database-path» URL (should replace the last part with the path you want) with the user 'sa' and an empty password.


How to start programming JDBC / HSQLDB

HSQLDB comes with documentation, example applets and source code that can help programers who are new to JDBC programming.

An example application is in the guide. Another example is FindFile.java in the /src/org/hsqldb/sample folder. This is a tool to search files. The source code of this demo application is well documented, and the functionality is kept low, so it should be easy to understand what is going on just reading the source code.

Because the source code of all other tools and applets is also included in the download, more advanced programmers will find some hints there. Check the sources in /src/org/hsqldb/sample and /src/org/hsqldb/test folders.


Where to get more Documentation

HSQLDB has a standard JDBC interface. HSQLDB specific JDBC documentation is included in the /doc/src folder.

There are also many books available on JDBC programming.

For people who are interested in the database engine design of HSQLDB, the source code is where to look.


How to use HSQLDB inside JBuilder, Eclipse, etc.

To use HSQLDB at design-time in JBuilder, Eclipse or other tools, you usually require the plug-in for databases that comes with the development environment. You usually need to add a reference to the HSQLDB jar to the environment. Also you normally need to register the JDBC driver (which is part of the hsqldb.jar) with the environment.


Selecting big results / Scanning big tables

One limitation of HSQLDB is that it currently does not support server side cursors. (This allows it to run without any writeable media). This means the result of a query must always fit in memory, otherwise an OutOfMemory error occurs. In the rare situation that a huge resultsets must be processed, then the following workaround can be used:

Limit the ResultSet using Statement.setMaxRows(1024), and select multiple 'smaller' blocks. If the table is for example 'CREATE TABLE Test(Id INT IDENTITY PRIMARY KEY, Name VARCHAR)' then the first block can be selected using 'SELECT * FROM Test'. The biggest ID should be recorded and the next block should be selected using 'SELECT * FROM Test WHERE Id>(biggest_id)' until no more records are returned. Don't forget to switch of the limit using setMaxRows(0).


Java Stored Procedures / Functions

Stored procedures are Java functions that are called directly from the SQL language or using an alias. Calling Java functions (directly or using the alias) requires that the Java class can be reached by the database (server). User defined stored procedures / functions must be placed in the classpath of the database server. For HSQLDB, stored procedures and functions are equal. The syntax for calling functions and stored procedures (inside a SELECT statement for example) is:

SELECT Value, "java.lang.Math.sqrt"(Value) AS SQRT FROM Test

It can be called also without the SELECT, using the command CALL. This makes more sense for 'Stored Procedures' but works for functions, too:

CALL "java.lang.Math.sqrt"(2.0)

The package must be provided, and the name must be written as one word, and inside double quotes (") because otherwise it is converted to uppercase (and not found).

An alias can be created using the command CREATE ALIAS:

CREATE ALIAS SQRT FOR "java.lang.Math.sqrt"

When an alias is defined, then the function can be called additionally using this alias:

CALL SQRT(2.0)
SELECT Value, SQRT(Value) AS SQRT FROM Test

These are the rules when creating user defined functions / stored procedures:

 

A security mechanism is provided for non-admin users: Access to each class can be granted / revoked to a user:

GRANT ALL ON "org.mysoftware.MyClass" TO PUBLIC

By default, all users have access to java.lang.Math and org.hsqldb.Library.


How to upgrade from an old version to the current version

An old version database can always be opened by the latest version and automatically converted if there is no CACHED table data in the database (the *.data file is empty or does not exist).

With CACHED tables, the procedures in the Deployment Issues section of the Guide should be followed.

Note that an upgrade is a one-way process, so please always keep a backup of the old database.


Reliability, Performance and Deployment

 

This text is based on original Hypersonic SQL documentation, updated to reflect the latest version of HSQLDB

SourceForge Logo

This page last updated 30 August 2005
Java is a Trademark of Sun Microsystems, Inc. (under various licenses). Contents of this page are ©2001-2009 The hsql Development Group. All rights reserved under the terms of the applicable Licenses.