Overview
This guide demonstrates how to set up and configure HSQLDB (HyperSQL Database) as the database engine for the enterprise management system. HSQLDB is a lightweight, embedded SQL database written in Java that provides full JDBC support.Prerequisites
Ensure you have the following before starting:
- Java Development Kit (JDK) 8 or higher
- HSQLDB JDBC driver (hsqldb-2.7.2.jar)
- Basic understanding of JDBC and SQL
Database Connection Setup
private static String URL;
private static final String USER = "SA";
private static final String PASSWORD = "";
private static void cargarConfiguracion() throws IOException {
File configFile = new File("config.ini");
Properties properties = new Properties();
if (!configFile.exists()) {
// Create default configuration
properties.setProperty("database.path", "./db_empresa");
properties.store(new FileOutputStream(configFile), "Configuración HSQLDB");
System.out.println("Archivo config.ini creado con ruta por defecto");
}
properties.load(new FileInputStream(configFile));
String dbPath = properties.getProperty("database.path", "./db_empresa");
URL = "jdbc:hsqldb:file:" + dbPath + ";shutdown=true;hsqldb.lock_file=false";
System.out.println("Conectando a: " + URL);
}
The connection string includes important parameters:
shutdown=true- Ensures the database shuts down cleanly when the last connection closeshsqldb.lock_file=false- Disables lock file creation for easier portability
public static void inicializarBaseDatos() {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
// Verify if tables already exist
DatabaseMetaData meta = conn.getMetaData();
ResultSet tables = meta.getTables(null, null, "T_CENTROS", null);
if (!tables.next()) {
// Tables don't exist, create them
System.out.println("Creando base de datos...");
crearEsquemaCompleto(conn);
} else {
System.out.println("Base de datos ya existe.");
}
} catch (SQLException e) {
System.out.println("Error al inicializar BD: " + e.getMessage());
}
}
Database Schema Creation
The complete schema includes three tables with foreign key relationships:Creating Tables
Inserting Sample Data
The application initializes the database with sample data:CRUD Operations
Query with JOIN
Complex JOIN Query
Query across all three tables to get complete employee information:Configuration File Format
Theconfig.ini file structure:
The database path can be:
- Relative:
./db_empresa(current directory) - Absolute:
/home/user/data/db_empresa - The
.script,.properties, and.logfiles will be created automatically
Running the Application
Archivo config.ini creado con ruta por defecto
Conectando a: jdbc:hsqldb:file:./db_empresa;shutdown=true;hsqldb.lock_file=false
Creando base de datos...
Base de datos creada exitosamente!
=== GESTIÓN EMPRESA ===
1. Ver empleados
2. Agregar empleado
3. Buscar empleados
4. Ver departamentos
5. Eliminar empleado
6. Información completa con JOINs
7. Salir
Database Files
After initialization, HSQLDB creates several files:db_empresa.script- Contains the schema and data in SQL formatdb_empresa.properties- Database properties and settingsdb_empresa.log- Transaction log for recoveryconfig.ini- Application configuration file
Advantages of HSQLDB
- Lightweight: Small footprint, perfect for embedded applications
- File-based: No separate server process required
- Standards-compliant: Full SQL support with JDBC
- Portable: Database files can be easily copied between systems
- Zero configuration: Works out of the box with minimal setup
Next Steps
- Learn about H2 Database Setup as an alternative
- See how to create a JAR deployment
- Review CRUD Operations documentation