关系型数据库(derby)

概述

用 Java 编写的关系数据库,可以两种模式运行:嵌入式或客户端服务器。遵循SQL标准并兼容JDBC接口。

maven依赖

xml 复制代码
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.11.1.1</version>
        </dependency>

示例

  • 内存模式
java 复制代码
    @Test
    void contextLoads() throws ClassNotFoundException, SQLException {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection connection = DriverManager.getConnection("jdbc:derby:memory:test;create=true");
        Statement statement = connection.createStatement();
        //创建表
        statement.execute("create table tb_demo(id integer not null, name varchar(50))");
        //插入数据
        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.apache.derby.jdbc.EmbeddedDriver");
        Connection connection = DriverManager.getConnection("jdbc:derby:E:\\db\\derby.db;create=true");
        Statement statement = connection.createStatement();
        //创建表
        statement.execute("create table tb_demo(id integer not null, name varchar(50))");
        //插入数据
        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);
        }
    }
  • 客户端服务器模式(需要先下载、安装、启动Derby网络服务器)
java 复制代码
    @Test
    void contextLoads1() throws ClassNotFoundException, SQLException {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection connection = DriverManager.getConnection("jdbc:derby://127.0.0.1:1527/testdb;create=true,"userName", "******");
        Statement statement = connection.createStatement();
        //创建表
        statement.execute("create table tb_demo(id integer not null, name varchar(50))");
        //插入数据
        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);
        }
    }

可参考文档

相关推荐
倔强的石头_3 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab3 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence4 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神4 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据5 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡5 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧5 天前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon5 天前
SQL学习指南——视图
数据库·sql
活宝小娜5 天前
mysql详细安装教程
数据库·mysql·adb