关系型数据库(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);
        }
    }

可参考文档

相关推荐
Yvonne爱编码3 分钟前
数据库---Day9 视图(附完整数据库脚本+练习题)
数据库·mysql·oracle
sukioe4 分钟前
Redis 入门:为什么出现、核心原理与安装配置
数据库·redis·缓存
宇砾8 分钟前
浅谈Redis(1)
数据库·redis·缓存
heimeiyingwang23 分钟前
【架构实战】Canal数据同步:MySQL数据变更实时捕获
数据库·mysql·架构
cdbqss125 分钟前
VB2026 动态生成工具栏类 BqGetToolStrip
数据库·oracle·开源·.net·学习方法·教育电商·basic
AI人工智能+电脑小能手28 分钟前
【大白话说Java面试题 第85题】【Mysql篇】第15题:MySQL 的事务中,幻读是怎么解决的?
java·开发语言·数据库·mysql·面试
yoothey34 分钟前
MySQL 索引小白面试详解
数据库·mysql
一 乐43 分钟前
在线考试|基于Springboot的在线考试管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·毕设·在线考试管理系统
玄米乌龙茶12344 分钟前
数据库与缓存核心概念
数据库·缓存
小陈的进阶之路1 小时前
MySQL 索引
数据库·mysql