Overview
TheDB_EnterpriseH2 class is a complete enterprise database management system using H2 Database Engine. It provides the same functionality as DB_EnterpriseHSQLDB but optimized for H2, including automatic database file cleanup and file-based storage.
Class Information
DB_EnterpriseH2JDBC Driver:
org.h2.DriverDatabase Constants
H2 database connection URL with auto-server mode and persistent connection
Enables multiple concurrent connections to the same database
Keeps database open until JVM shutdown (-1 = never auto-close)
Database username constant
Database password (empty by default)
Main Method
Signature
Workflow
Core Methods
eliminarArchivosBD()
Purpose
Deletes existing H2 database files before creating a fresh database
Files Removed
db_empresa_h2.mv.db- Main database filedb_empresa_h2.trace.db- Trace/log file
Implementation
Implementation
inicializarBaseDatos()
Purpose
Checks if tables exist and creates the complete schema if needed
Implementation
Implementation
crearEsquemaCompleto()
Active H2 database connection
Tables Created
t_centros- Work centers/locationst_departamentos- Departments with foreign key to centerst_empleados- Employees with foreign key to departments
Schema Structure
Schema Structure
menuPrincipal()
- Ver empleados
- Agregar empleado
- Buscar empleados
- Ver departamentos
- Eliminar empleado
- Información completa con JOINs
- Salir
CRUD Operations
verEmpleados()
SQL Query
SQL Query
agregarEmpleado()
- Código (Employee ID)
- Departamento (Department ID)
- Nombre (Employee Name)
- Salario (Base Salary)
- Fecha ingreso (Hire Date in YYYY-MM-DD format)
Parameterized Insert
Parameterized Insert
buscarEmpleados()
Partial name for LIKE search
Search Implementation
Search Implementation
verDepartamentos()
Output
Lists all departments with ID, name, and budget information
Query
Query
eliminarEmpleado()
Employee code to delete from the database
Delete Implementation
Delete Implementation
informacionCompletaJoins()
Purpose
Displays comprehensive employee information by joining three tables
Triple JOIN Query
Triple JOIN Query
Joins
- Employee → Department (via codigo_departamento)
- Department → Center (via codigo_centro)
Sample Data
The database is populated with initial data during creation:Centers (t_centros)
Centers (t_centros)
Departments (t_departamentos)
Departments (t_departamentos)
Eight departments including:
- 100: DIRECCION GENERAL (Budget: 120,000)
- 110: DIRECCION COMERCIAL (Budget: 15,000)
- 111: SECTOR INDUSTRIAL (Budget: 11,000)
- 112: SECTOR SERVICIOS (Budget: 9,000)
- 120: ORGANIZACION (Budget: 3,000)
- 121: PERSONAL (Budget: 2,000)
- 122: PROCESO DE DATOS (Budget: 6,000)
- 130: FINANZAS (Budget: 2,000)
Employees (t_empleados)
Employees (t_empleados)
Six employees with complete information:
- 110: PONS, CESAR (Salary: 3,100, Dept: 121 PERSONAL)
- 120: LASA, MARIO (Salary: 3,500, Dept: 112 SECTOR SERVICIOS)
- 130: TEROL, LUCIANO (Salary: 2,900, Dept: 112 SECTOR SERVICIOS)
- 150: PEREZ, JULIO (Salary: 4,400, Dept: 121 PERSONAL)
- 160: AGUIRRE, AUREO (Salary: 3,100, Dept: 111 SECTOR INDUSTRIAL)
- 180: PEREZ, MARCOS (Salary: 4,800, Dept: 110 DIRECCION COMERCIAL)
H2-Specific Features
Database stored in
db_empresa_h2.mv.db fileAllows multiple JVMs to connect to the same database file simultaneously
Prevents automatic database closure when last connection closes
Error Handling
Differences from HSQLDB Version
| Feature | DB_EnterpriseH2 | DB_EnterpriseHSQLDB |
|---|---|---|
| Driver | org.h2.Driver | org.hsqldb.jdbc.JDBCDriver |
| URL Format | jdbc:h2:file | jdbc:hsqldb:file |
| Auto Server | AUTO_SERVER=TRUE | N/A |
| File Cleanup | eliminarArchivosBD() | No cleanup |
| Config File | Hardcoded URL | config.ini |
| Table Check | T_EMPLEADOS | T_CENTROS |
Related Classes
- DB_Enterprise - Basic configuration manager
- DB_EnterpriseHSQLDB - HSQLDB implementation