Embed Database
database, java, xml at August 2nd, 2004 by 小影
Introduction
Most business application handle data: receive data, process data, store data and output data. Most programmer's job are "simply" handle data well. One might think using text based file(ini, properties, xml) and/or binary file(java serialized format or other custom format), and then access and manupulate them data in memory. They have their advantages, but when you need to handle thousands of records or more, you would eventually have to consider using a relational database management system (RDBMS).
Relational Database Management System
Using RDBMS you can store your data safely, ensure their integrity and access them with handy and powerful SQL commands. On the other hand it might not be a good idea to setup a database server or create an ODBC datasource just for a small icq like system. In this case, embed a small database engine in your application is the answer.
"Embed a database server in your application" might sound terribly difficult and expensive, but with java and open source community we can make it simpler. "HSQLDB is a relational database engine written in Java, with a JDBC driver, supporting a subset of ANSI-92 SQL. It is a small, fast database engine supporting both in-memory and disk-based tables." -- From the user guide of HSQLDB. For more information of HSQLDB check their sourceforge website.
Operating Modes of hsqldb
There are serveral way to run hsqldb, they can be divided into server/client modes and standalone mode.
Server mode run in a separate instance, and client connect with the server through network just like other RDBMS. HSQLDB provide socket server which using proprietary communications protocol, which is the prefered way. When network is limited by a firewall, HTTP based and Servlet based server can be used.
Standalone mode is suggested in embed applications. It start a server in the client application, without the overhead of networks.
Running hsqldb
To start a standalone server is extremely easy, download the hsqldb packages and add
Class.forName("org.hsqldb.jdbcDriver");
java.sql.Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "sa", "");
. . . and thats it, you can then access the testdb with the connection c, just like any other JDBC datasource. In this case if you have no existing database file in the location, 2-5 files are created.
- testdb.properties
- testdb.script
- testdb.log
- testdb.data
- testdb.backup
The properties files contains general settings about the database. The script file contains the definition of tables and other database objects, plus the data for non-cached tables. The log file contains recent changes to the database. The data file contains the data for cached tables and the backup file is a zipped backup of the last known consistent state of the data file. All these files are essential and should never be deleted. If the database has no cached tables, the test.data and test.backup files will not be present. In addition to those files, HSQLDB database may link to any formatted text files, such as CSV lists, anywhere on the disk. -- from hsqldb user guide.
If you need a database in your program and stuck in managing the DBMS, why dont try HSQLDB right now?