关系型数据库(h2)

概述

纯Java实现开源关系数据库,支持嵌入式、服务器和混合三种运行模式,具备轻量化(约2MB JAR文件)和跨平台特性。该数据库采用多版本并发控制(MVCC)机制优化并发访问性能,支持SQL标准的大部分功能,并兼容MySQL语法。

maven依赖

xml 复制代码
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.210</version>
</dependency>

示例

  • 内存模式
java 复制代码
    @Test
    void contextLoads() throws ClassNotFoundException, SQLException {
        Class.forName("org.h2.Driver");
        Connection connection = DriverManager.getConnection("jdbc:h2:mem:test");
        Statement statement = connection.createStatement();
        //创建表
        statement.execute("create table tb_demo(id integer not null, name TEXT);");
        //插入数据
        statement.execute("insert into tb_demo(id,name) values (1,'bob');");
        statement.execute("insert into tb_demo(id,name) values (2,'pop');");
        //查询数据
        ResultSet resultSet = statement.executeQuery("select * from tb_demo");
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + "   name: " + name);
        }
    }
  • 文件模式
java 复制代码
    @Test
    void contextLoads1() throws ClassNotFoundException, SQLException {
        Class.forName("org.h2.Driver");
        //文件模式,会将数据库文件存储在指定位置。配置数据库文件生成会自动加 .mv.db 后缀名
        Connection connection = DriverManager.getConnection("jdbc:h2:file:E:\\db\\h2");
        Statement statement = connection.createStatement();
        //创建表
        statement.execute("create table tb_demo(id integer not null, name TEXT);");
        //插入数据
        statement.execute("insert into tb_demo(id,name) values (1,'bob');");
        statement.execute("insert into tb_demo(id,name) values (2,'pop');");
        //查询数据
        ResultSet resultSet = statement.executeQuery("select * from tb_demo");
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + "   name: " + name);
        }
    }

可参考文档

相关推荐
星星也在雾里1 天前
PgBouncer 解决 PostgreSQL 连接数超限 + 可视化监控
数据库·postgresql
雨辰AI1 天前
SpringBoot3 + 人大金仓读写分离 + 分库分表 + 集群高可用 全栈实战
java·数据库·mysql·政务
长城20241 天前
关于MySql的ONLY_FULL_GROUP_BY问题
数据库·mysql·聚合列
常常有1 天前
MySQL 底层执行原理:输入SQL语句到两阶段提交
数据库·sql·mysql
Mr. zhihao1 天前
深入解析redis基本数据结构
数据结构·数据库·redis
m0_748839491 天前
利用天正暖通CAD快速掌握风管数量统计的方法
数据库
随身数智备忘录1 天前
什么是设备管理体系?设备管理体系包含哪些核心模块?
网络·数据库·人工智能
海市公约1 天前
MySQL更新语句执行全流程:从Buffer Pool修改到二阶段提交
数据库·mysql·binlog·innodb·undo log·二阶段提交·update执行原理
颂love1 天前
MySQL的执行流程
android·数据库·mysql
程序leo源1 天前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#