Skip to main content

Get up and running quickly

This quickstart guide will help you run the H2 database enterprise management system and perform your first database operations.
1
Download the H2 JDBC Driver
2
First, download the H2 database driver JAR file:
3
wget https://repo1.maven.org/maven2/com/h2database/h2/2.2.224/h2-2.2.224.jar
4
Or manually download from the H2 Database website.
5
Compile the Application
6
Navigate to the source directory and compile the main application:
7
cd Acceso_Datos_Tarea2
javac -cp ".:h2-2.2.224.jar" DB_EnterpriseH2.java
8
On Windows, use semicolon instead of colon: -cp ".;h2-2.2.224.jar"
9
Run the Application
10
Execute the compiled program:
11
java -cp ".:h2-2.2.224.jar" DB_EnterpriseH2
12
You’ll see the H2 database initialize with sample data:
13
Archivo de BD anterior eliminado: db_empresa_h2.mv.db
Creando base de datos H2...
Base de datos H2 en memoria creada exitosamente!

=== GESTIÓN EMPRESA (H2) ===
1. Ver empleados
2. Agregar empleado
3. Buscar empleados
4. Ver departamentos
5. Eliminar empleado
6. Información completa con JOINs
7. Salir
Elige:
14
Explore the Features
15
Try out the menu options:
16
View all employees
# Select option 1
1

# Output:
--- EMPLEADOS ---
ID: 110 | Nombre: PONS, CESAR | Depto: PERSONAL | Salario: 3100.0 | Ingreso: 1970-02-15
ID: 120 | Nombre: LASA, MARIO | Depto: SECTOR SERVICIOS | Salario: 3500.0 | Ingreso: 1988-10-01
...
Add a new employee
# Select option 2
2

# Follow the prompts:
Código: 200
Departamento: 121
Nombre: GARCIA, ANA
Salario: 3200
Fecha ingreso (YYYY-MM-DD): 2024-01-15

# Output:
Empleado agregado!
Search employees
# Select option 3
3

# Enter search term:
Buscar por nombre: PEREZ

# Output:
--- RESULTADOS ---
ID: 150 | Nombre: PEREZ, JULIO | Salario: 4400.0 | Ingreso: 1968-01-15
ID: 180 | Nombre: PEREZ, MARCOS | Salario: 4800.0 | Ingreso: 1976-03-18

Understanding the Application

The application demonstrates key JDBC concepts:

Database Schema

Three interconnected tables: t_centros (centers), t_departamentos (departments), and t_empleados (employees)

CRUD Operations

Full Create, Read, Update, Delete functionality with PreparedStatements for security

JOIN Queries

Complex queries combining employee, department, and center information

H2 File Database

Persistent file-based database that survives application restarts

Key Code Snippets

Database Connection

The application uses H2’s file-based database with auto-server mode:
DB_EnterpriseH2.java:7-9
private static final String URL = "jdbc:h2:file:./db_empresa_h2;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1";
private static final String USER = "SA";
private static final String PASSWORD = "";

PreparedStatement for Security

All user inputs use PreparedStatements to prevent SQL injection:
DB_EnterpriseH2.java:220-226
String sql = "SELECT * FROM t_empleados WHERE nombre_empleado LIKE ?";

try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
     PreparedStatement pstmt = conn.prepareStatement(sql)) {
    
    pstmt.setString(1, "%" + nombre + "%");
    ResultSet rs = pstmt.executeQuery();

Multi-table JOIN Query

Retrieving complete employee information across three tables:
DB_EnterpriseH2.java:260-272
String sql = "SELECT " +
            "e.codigo_empleado, " +
            "e.nombre_empleado, " +
            "e.salario_base_empleado, " +
            "d.nombre_departamento, " +
            "c.nombre_centro, " +
            "c.ubicacion " +
            "FROM t_empleados e " +
            "JOIN t_departamentos d ON e.codigo_departamento = d.codigo_departamento " +
            "JOIN t_centros c ON d.codigo_centro = c.codigo_centro " +
            "ORDER BY e.codigo_empleado";
The application deletes and recreates the database file on each startup. This is intentional for the tutorial but not suitable for production use.

Next Steps

Installation Guide

Detailed setup instructions for Java and database drivers

Database Configuration

Learn about config.ini and connection settings

CRUD Operations

Deep dive into database operations

HSQLDB Setup

Try the alternative HSQLDB implementation

Troubleshooting

Make sure the H2 JAR file is in your classpath when compiling and running:
java -cp ".:h2-2.2.224.jar" DB_EnterpriseH2
If you see locking errors, ensure no other process is using the database. The AUTO_SERVER=TRUE parameter helps multiple connections, but the app recreates the DB on startup.
Ensure you’re using Java 8 or higher:
java -version
javac -version

Build docs developers (and LLMs) love