CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-h2database--h2

H2 Database Engine - A very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

H2 Database Engine

H2 Database Engine is a very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint. H2 supports both in-memory and persistent databases, making it ideal for development, testing, embedded applications, and production systems requiring a lightweight yet powerful SQL database solution.

Package Information

  • Package Name: com.h2database:h2
  • Package Type: Maven
  • Language: Java
  • Installation: Add to Maven pom.xml:
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>2.3.232</version>
    </dependency>
  • Gradle: implementation 'com.h2database:h2:2.3.232'

Core Imports

import org.h2.Driver;
import org.h2.jdbcx.JdbcDataSource;

For JDBC usage:

import java.sql.*;

For tools and utilities:

import org.h2.tools.*;

Basic Usage

Simple Embedded Database

import java.sql.*;

public class H2Example {
    public static void main(String[] args) throws SQLException {
        // Register H2 driver (optional in modern Java)
        Class.forName("org.h2.Driver");
        
        // Create in-memory database connection
        Connection conn = DriverManager.getConnection(
            "jdbc:h2:mem:testdb", "sa", "");
        
        // Create table and insert data
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))");
        stmt.execute("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob')");
        
        // Query data
        ResultSet rs = stmt.executeQuery("SELECT * FROM users");
        while (rs.next()) {
            System.out.println("ID: " + rs.getInt("id") + 
                             ", Name: " + rs.getString("name"));
        }
        
        // Close connection
        conn.close();
    }
}

Persistent Database

// File-based database (creates testdb.mv.db file)
Connection conn = DriverManager.getConnection(
    "jdbc:h2:~/testdb", "sa", "password");

Using DataSource

import org.h2.jdbcx.JdbcDataSource;

JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:testdb");
ds.setUser("sa");
ds.setPassword("");

Connection conn = ds.getConnection();

Architecture

H2 Database Engine is built around several key components:

  • JDBC Driver: Standard JDBC 4.2 compliant driver for database connectivity
  • Database Engine: Core SQL processing engine with full ACID transaction support
  • MVStore: Advanced storage engine providing MVCC (Multi-Version Concurrency Control)
  • Server Components: TCP server, Web console, and PostgreSQL compatibility server
  • Administrative Tools: Command-line utilities for backup, restore, scripting, and maintenance
  • Extension APIs: Interfaces for custom triggers, aggregates, table engines, and event listeners

Capabilities

JDBC Connectivity

Standard JDBC database connectivity with H2-specific enhancements for embedded and server modes. Supports connection pooling, XA transactions, and various authentication methods.

public class Driver implements java.sql.Driver {
    public Connection connect(String url, Properties info) throws SQLException;
    public boolean acceptsURL(String url) throws SQLException;
    public static Driver load();
}
public class JdbcDataSource implements DataSource, XADataSource, ConnectionPoolDataSource {
    public Connection getConnection() throws SQLException;
    public Connection getConnection(String username, String password) throws SQLException;
    public void setURL(String url);
    public void setUser(String user);
    public void setPassword(String password);
}

JDBC Connectivity

Administrative Tools

Comprehensive set of command-line tools for database administration, maintenance, and data management operations.

public class Server extends Tool implements Runnable {
    public static void main(String... args);
    public Server();
    public Server(Service service, String... args);
}
public class Script extends Tool {
    public static void main(String... args);
}
public class Backup extends Tool {
    public static void main(String... args);
}

Tools and Utilities

Embedded MVStore

High-performance key-value storage engine that can be used independently of the SQL database. Provides persistent maps with ACID transactions and MVCC.

public class MVStore implements AutoCloseable {
    public static MVStore open(String fileName);
    public <K, V> MVMap<K, V> openMap(String name);
    public Set<String> getMapNames();
    public void close();
}
public class MVMap<K, V> {
    public V put(K key, V value);
    public V get(Object key);
    public V remove(Object key);
    public long size();
}

Embedded MVStore

Server Deployment

Server components for deploying H2 as a standalone database server with support for multiple client connections, web-based administration, and PostgreSQL protocol compatibility.

public class TcpServer {
    // TCP/IP database server implementation
}
public class WebServer {
    // HTTP server for H2 Console web interface
}
public class PgServer {
    // PostgreSQL protocol compatibility server
}

Server Deployment

Extension APIs

Interfaces for extending H2 Database functionality with custom triggers, aggregate functions, table engines, and event listeners.

public interface Trigger {
    int INSERT = 1;
    int UPDATE = 2;
    int DELETE = 4;
    int SELECT = 8;
    
    void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException;
    default void init(Connection conn, String schemaName, String triggerName, 
                     String tableName, boolean before, int type) throws SQLException;
}
public interface AggregateFunction {
    void add(Object value) throws SQLException;
    Object getResult() throws SQLException;
    int getType(int[] inputTypes) throws SQLException;
}
public interface DatabaseEventListener extends EventListener {
    default void exceptionThrown(SQLException e, String sql);
    default void setProgress(int state, String name, long x, long max);
}

Extension APIs

Common Connection URLs

  • In-memory database: jdbc:h2:mem:dbname
  • File database: jdbc:h2:~/dbname or jdbc:h2:./dbname
  • Encrypted database: jdbc:h2:~/secure;CIPHER=AES
  • TCP server: jdbc:h2:tcp://localhost/~/dbname
  • SSL connection: jdbc:h2:ssl://localhost/~/dbname
  • Mixed mode: jdbc:h2:~/dbname;AUTO_SERVER=TRUE
  • Read-only: jdbc:h2:~/dbname;ACCESS_MODE_DATA=r

Error Handling

H2 uses standard SQLException hierarchy with H2-specific error codes defined in ErrorCode class:

public class ErrorCode {
    public static final int NO_DATA_AVAILABLE = 2000;
    // ... numerous other error codes
}

Common patterns:

try {
    Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
    // database operations
} catch (SQLException e) {
    System.err.println("Error Code: " + e.getErrorCode());
    System.err.println("SQL State: " + e.getSQLState());
    System.err.println("Message: " + e.getMessage());
}

docs

extensions.md

index.md

jdbc.md

mvstore.md

server.md

tools.md

tile.json