计算
弹性云服务器 ECS
Flexus云服务
裸金属服务器 BMS
弹性伸缩 AS
镜像服务 IMS
专属主机 DeH
函数工作流 FunctionGraph
云手机服务器 CPH
Huawei Cloud EulerOS
网络
虚拟私有云 VPC
弹性公网IP EIP
虚拟专用网络 VPN
弹性负载均衡 ELB
NAT网关 NAT
云专线 DC
VPC终端节点 VPCEP
云连接 CC
企业路由器 ER
企业交换机 ESW
全球加速 GA
安全与合规
安全技术与应用
Web应用防火墙 WAF
企业主机安全 HSS
云防火墙 CFW
安全云脑 SecMaster
DDoS防护 AAD
数据加密服务 DEW
数据库安全服务 DBSS
云堡垒机 CBH
数据安全中心 DSC
云证书管理服务 CCM
边缘安全 EdgeSec
CDN与智能边缘
内容分发网络 CDN
CloudPond云服务
智能边缘云 IEC
迁移
主机迁移服务 SMS
对象存储迁移服务 OMS
云数据迁移 CDM
迁移中心 MGC
大数据
MapReduce服务 MRS
数据湖探索 DLI
表格存储服务 CloudTable
云搜索服务 CSS
数据接入服务 DIS
数据仓库服务 GaussDB(DWS)
数据治理中心 DataArts Studio
数据可视化 DLV
数据湖工厂 DLF
湖仓构建 LakeFormation
企业应用
云桌面 Workspace
应用与数据集成平台 ROMA Connect
云解析服务 DNS
专属云
专属计算集群 DCC
IoT物联网
IoT物联网
设备接入 IoTDA
智能边缘平台 IEF
云生态
合作伙伴中心
云商店
开发者工具
SDK开发指南
API签名指南
Terraform
华为云命令行工具服务 KooCLI
其他
产品价格详情
系统权限
管理控制台
客户关联华为云合作伙伴须知
消息中心
公共问题
视频
视频直播 Live
视频点播 VOD
媒体处理 MPC
实时音视频 SparkRTC
数字内容生产线 MetaStudio
开发与运维
应用管理与运维平台 ServiceStage
软件开发生产线 CodeArts
需求管理 CodeArts Req
部署 CodeArts Deploy
性能测试 CodeArts PerfTest
编译构建 CodeArts Build
流水线 CodeArts Pipeline
制品仓库 CodeArts Artifact
测试计划 CodeArts TestPlan
代码检查 CodeArts Check
代码托管 CodeArts Repo
云应用引擎 CAE
华为云Astro轻应用
华为云Astro大屏应用
开源治理服务 CodeArts Governance
存储
对象存储服务 OBS
云硬盘 EVS
云备份 CBR
存储容灾服务 SDRS
高性能弹性文件服务 SFS Turbo
弹性文件服务 SFS
云硬盘备份 VBS
云服务器备份 CSBS
数据快递服务 DES
专属分布式存储服务 DSS
容器
云容器引擎 CCE
容器镜像服务 SWR
应用服务网格 ASM
华为云UCS
云容器实例 CCI
管理与监管
云监控服务 CES
统一身份认证服务 IAM
资源编排服务 RFS
云审计服务 CTS
标签管理服务 TMS
云日志服务 LTS
配置审计 Config
资源访问管理 RAM
消息通知服务 SMN
应用运维管理 AOM
应用性能管理 APM
组织 Organizations
优化顾问 OA
IAM 身份中心
云运维中心 COC
资源治理中心 RGC
应用身份管理服务 OneAccess
数据库
云数据库 RDS
文档数据库服务 DDS
数据管理服务 DAS
数据复制服务 DRS
云数据库 GeminiDB
云数据库 GaussDB
分布式数据库中间件 DDM
数据库和应用迁移 UGO
云数据库 TaurusDB
人工智能
人脸识别服务 FRS
图引擎服务 GES
图像识别 Image
内容审核 Moderation
文字识别 OCR
AI开发平台ModelArts
图像搜索 ImageSearch
对话机器人服务 CBS
华为HiLens
视频智能分析服务 VIAS
语音交互服务 SIS
应用中间件
分布式缓存服务 DCS
API网关 APIG
微服务引擎 CSE
分布式消息服务Kafka版
分布式消息服务RabbitMQ版
分布式消息服务RocketMQ版
多活高可用服务 MAS
事件网格 EG
企业协同
华为云会议 Meeting
云通信
消息&短信 MSGSMS
云化转型
云架构中心
云采用框架
用户服务
账号中心
费用中心
成本中心
资源中心
企业管理
工单管理
国际站常见问题
ICP备案
我的凭证
支持计划
客户运营能力
合作伙伴支持计划
专业服务
区块链
区块链服务 BCS
Web3节点引擎服务 NES
解决方案
SAP
高性能计算 HPC
开天aPaaS
云消息服务 KooMessage
云手机服务 KooPhone
云空间服务 KooDrive
本文导读

展开导读

使用Struct类型

更新时间:2025-05-29 GMT+08:00

以下示例将演示如何使用JDBC驱动的Struct类型。

代码运行的前提条件:

  1. 根据实际情况添加gaussdbjdbc.jar包(例如,用户使用IDE执行代码,则需要在本地IDE添加gaussdbjdbc.jar包)。
  2. 连接的数据库兼容模式为ORA,数据库版本需要大于等于503.0。

示例一,JDBC驱动使用内核record类型基础示例:

// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放,使用时解密),确保安全。
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
// $ip、$port、database需要用户自行修改。

import com.huawei.gaussdb.jdbc.jdbc.StructDescriptor;
import com.huawei.gaussdb.jdbc.jdbc.GaussStruct;

import java.sql.*;

public class StructTest1 {
    // 以非加密方式创建数据库连接。
    public static Connection getConnection() {
        String username = System.getenv("EXAMPLE_USERNAME_ENV");
        String passwd = System.getenv("EXAMPLE_PASSWORD_ENV");
        String driver = "com.huawei.gaussdb.jdbc.Driver";
        String sourceURL = "jdbc:gaussdb://$ip:$port/database?enableGaussArrayAndStruct=true";
        Connection conn = null;
        try {
            // 加载数据库驱动。
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            // 创建数据库连接。
            conn = DriverManager.getConnection(sourceURL, username, passwd);
            System.out.println("Connection succeed!");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return conn;
    }

    /**
     * 创建前置数据库对象。
     *
     * @param conn conn
     * @throws SQLException An exception occurred while executing the statement
     */
    public static void prepareTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        // 支持出参,需要数据库开启proc_outparam_override参数。
        stmt.execute("set behavior_compat_options = 'proc_outparam_override'");
        // 创建测试对象。
        stmt.execute("create or replace package test_pkg is\n" +
                "    type test_rec is record(col1 int, col2 varchar(50));\n" +
                "    function test_func return test_rec;\n" +
                "    procedure test_proc(v1 in test_rec, v2 out test_rec);\n" +
                "end test_pkg;");
        stmt.execute("create or replace package body test_pkg is\n" +
                "    function test_func return test_rec is\n" +
                "        v test_rec;" +
                "    begin\n" +
                "        v.col1 := 123;\n" +
                "        v.col2 := 'abc';\n" +
                "        return v;\n" +
                "    end;\n" +
                "    procedure test_proc(v1 in test_rec, v2 out test_rec) is\n" +
                "    begin\n" +
                "        v2.col1 := v1.col1 + 1;\n" +
                "        v2.col2 := v1.col2 || 'd';\n" +
                "    end;\n" +
                "end test_pkg;");
    }

    /**
     * 清理数据库对象。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred while executing the statement
     */
    public static void cleanTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        stmt.execute("drop package test_pkg");
    }

    /**
     * 使用struct对象并的相关接口。
     *
     * @param struct instance of Struct
     * @throws SQLException if used Struct failed
     */
    public static void testStruct(Struct struct) throws SQLException {
        // 遍历打印元素。
        Object[] attributes = struct.getAttributes();
        for (Object attribute : attributes) {
            System.out.println(attribute);
        }
    }

    /**
     * 构造Struct对象并使用其相关接口, 非标准构造方法。
     *
     * @param conn conn
     * @throws SQLException if create Struct failed
     */
    public static void testConstructStruct1(Connection conn) throws SQLException {
        System.out.println("===========  testConstructStruct1  ===========");
        String typeName = "test_pkg.test_rec";
        StructDescriptor typeDesc = StructDescriptor.getDescriptor(typeName, conn);
        GaussStruct struct = new GaussStruct(typeDesc, new Object[]{666, "aaabbbccc"});
        testStruct(struct);
    }

    /**
     * 构造Struct对象并使用其相关接口, 标准接口构造。
     *
     * @param conn conn
     * @throws SQLException if create Struct failed
     */
    public static void testConstructStruct2(Connection conn) throws SQLException {
        System.out.println("===========  testConstructStruct2  ===========");
        String typeName = "test_pkg.test_rec";
        Struct struct = conn.createStruct(typeName, new Object[]{666, "aaabbbccc"});
        testStruct(struct);
    }

    /**
     * 获取function返回的Struct对象。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred
     */
    public static void testReturnStructParam(Connection conn) throws SQLException {
        System.out.println("===========  testReturnStructParam  ===========");

        // 执行存储过程,获取函数返回的Struct对象并执行Struct相关接口。
        PreparedStatement stmt = conn.prepareStatement("select test_pkg.test_func()");
        ResultSet rs = stmt.executeQuery();
        rs.next();
        Struct Struct = (Struct) rs.getObject(1);
        testStruct(Struct);
    }

    /**
     * 构造Struct对象并执行入参和出参。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred
     */
    public static void testInputOutputStructParam(Connection conn) throws SQLException {
        System.out.println("===========  testInputOutputStructParam  ===========");
        String typeName = "test_pkg.test_rec";
        StructDescriptor typeDesc = StructDescriptor.getDescriptor(typeName, conn);
        GaussStruct inStruct = new GaussStruct(typeDesc, new Object[]{123, "abc"});

        // 执行存储过程。
        CallableStatement cstmt = conn.prepareCall("{call test_pkg.test_proc(?, ?)}");
        cstmt.setObject(1, inStruct);
        cstmt.registerOutParameter(2, Types.STRUCT, typeName);
        cstmt.execute();

        // 获取出参并执行Struct相关接口。
        Struct outStruct = (Struct) cstmt.getObject(2);
        testStruct(outStruct);
    }

    /**
     * 主程序,逐步调用各静态方法。
     *
     * @param args args
     */
    public static void main(String[] args) throws SQLException {
        // 创建数据库连接。
        Connection conn = getConnection();

        // 创建前置测试对象。
        prepareTestObject(conn);

        // 构造Struct对象方式1。
        testConstructStruct1(conn);

        // 构造Struct对象方式2。
        testConstructStruct2(conn);

        // Struct返回值。
        testReturnStructParam(conn);

        // Struct入参和出参。
        testInputOutputStructParam(conn);

        // 删除测试对象。
        cleanTestObject(conn);
    }
}

上述示例的运行结果为:

===========  testConstructStruct1  ===========
666
aaabbbccc
===========  testConstructStruct2  ===========
666
aaabbbccc
===========  testReturnStructParam  ===========
123
abc
===========  testInputOutputStructParam  ===========
124
abcd

示例二,Struct对象接口使用基础示例:

// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放,使用时解密),确保安全。
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
// $ip、$port、database需要用户自行修改。

import com.huawei.gaussdb.jdbc.jdbc.GaussStruct;
import com.huawei.gaussdb.jdbc.jdbc.StructDescriptor;

import java.sql.*;

public class StructTest2 {
    // 以非加密方式创建数据库连接。
    public static Connection getConnection() {
        String username = System.getenv("EXAMPLE_USERNAME_ENV");
        String passwd = System.getenv("EXAMPLE_PASSWORD_ENV");
        String driver = "com.huawei.gaussdb.jdbc.Driver";
        String sourceURL = "jdbc:gaussdb://$ip:$port/database?enableGaussArrayAndStruct=true";
        Connection conn = null;
        try {
            // 加载数据库驱动。
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            // 创建数据库连接。
            conn = DriverManager.getConnection(sourceURL, username, passwd);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return conn;
    }

    /**
     * 创建前置数据库对象。
     *
     * @param conn conn
     * @throws SQLException An exception occurred while executing the statement
     */
    public static void prepareTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        stmt.execute("create or replace package test_pkg is\n" +
                "    type test_rec is record(col1 int, col2 varchar(50));\n" +
                "end test_pkg;");
    }

    /**
     * 清理数据库对象。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred while executing the statement
     */
    public static void cleanTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        stmt.execute("drop package test_pkg");
    }

    /**
     * 主程序,逐步调用各静态方法。
     *
     * @param args args
     */
    public static void main(String[] args) throws SQLException {
        // 创建数据库连接。
        Connection conn = getConnection();

        // 创建前置测试对象。
        prepareTestObject(conn);

        String typeName = conn.getSchema() + ".test_pkg.test_rec";
        // 获取test_array类型的类型描述符。
        StructDescriptor desc = StructDescriptor.getDescriptor(typeName, conn);

        // 根据类型描述符和元素数据创建GaussStruct对象。
        GaussStruct struct = new GaussStruct(desc, new Object[]{123, "abc"});

        // 获取record的类型描述符。
        desc = struct.getDescriptor();

        // 类型描述符相关接口使用, 非标准接口。
        // 打印类型名是否等于$currentSchema.test_pkg.test_rec
        System.out.println(typeName.equals(desc.getSQLTypeName()));
        // 打印类型的sqlType是否等于Types.STRUCT。
        System.out.println(desc.getSQLType() == Types.STRUCT);

        // Struct相关接口使用。
        // 获取并遍历struct的元素。
        Object[] attributes = struct.getAttributes();
        for (Object attribute : attributes) {
            System.out.println(attribute);
        }

        // 删除测试对象。
        cleanTestObject(conn);
    }
}

上述示例的运行结果为:

true
true
123
abc

示例三,元素类型为字符类型时,元素为空串,在传给数据库时会转换为null:

// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放,使用时解密),确保安全。
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
// $ip、$port、database需要用户自行修改。

import java.sql.*;

public class StructTest3 {
    // 以非加密方式创建数据库连接。
    public static Connection getConnection() {
        String username = System.getenv("EXAMPLE_USERNAME_ENV");
        String passwd = System.getenv("EXAMPLE_PASSWORD_ENV");
        String driver = "com.huawei.gaussdb.jdbc.Driver";
        String sourceURL = "jdbc:gaussdb://$ip:$port/database?enableGaussArrayAndStruct=true";
        Connection conn = null;
        try {
            // 加载数据库驱动。
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        try {
            // 创建数据库连接。
            conn = DriverManager.getConnection(sourceURL, username, passwd);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return conn;
    }

    /**
     * 创建前置数据库对象。
     *
     * @param conn conn
     * @throws SQLException An exception occurred while executing the statement
     */
    public static void prepareTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        stmt.execute("create table test_tab(c1 varchar(50))");

        stmt.execute("create or replace package test_pkg is\n" +
                "    type test_rec is record(col1 varchar(50), col2 varchar(50), col3 varchar(50));\n" +
                "    procedure test_proc(v1 in test_rec);\n" +
                "end test_pkg;");

        stmt.execute("create or replace package body test_pkg is\n" +
                "    procedure test_proc(v1 in test_rec) is\n" +
                "    begin\n" +
                "        insert into test_tab values(v1.col1);\n" +
                "        insert into test_tab values(v1.col2);\n" +
                "        insert into test_tab values(v1.col3);\n" +
                "    end;\n" +
                "end test_pkg;");
    }

    /**
     * 清理数据库对象。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred while executing the statement
     */
    public static void cleanTestObject(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        stmt.execute("drop package test_pkg");
        stmt.execute("drop table test_tab");
    }

    /**
     * 将带有空元素的Struct传给数据库。
     *
     * @param conn conn
     * @throws SQLException if an exception occurred while executing the statement
     */
    public static void inParamWithEmptyElement(Connection conn) throws SQLException {
        String typeName = "test_pkg.test_rec";
        // 创建Struct对象。
        Struct struct = conn.createStruct(typeName, new Object[]{"", "", null});
        // 遍历并打印元素。
        for (Object attribute : struct.getAttributes()) {
            if (attribute == null) {
                System.out.println("attribute is null");
            } else {
                if (((String) attribute).isEmpty()) {
                    System.out.println("attribute is empty");
                }
            }
        }

        // 传入入参并执行存储过程。
        CallableStatement cstmt = conn.prepareCall("{call test_pkg.test_proc(?)}");
        cstmt.setObject(1, struct, Types.STRUCT);
        cstmt.execute();

        // 查询结果。
        PreparedStatement stmt = conn.prepareStatement("select * from test_tab");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            String s = rs.getString(1);
            if (s == null) {
                System.out.println("s is null");
            } else {
                if (s.isEmpty()) {
                    System.out.println("s is empty");
                }
            }
        }
    }

    /**
     * 主程序,逐步调用各静态方法。
     *
     * @param args args
     */
    public static void main(String[] args) throws SQLException {
        // 创建数据库连接。
        Connection conn = getConnection();

        // 创建前置测试对象。
        prepareTestObject(conn);

        // 将带有空元素的struct作为入参传给数据库。
        inParamWithEmptyElement(conn);

        // 删除测试对象。
        cleanTestObject(conn);
    }
}

上述示例的运行结果为:

attribute is empty
attribute is empty
attribute is null
s is null
s is null
s is null
须知:
  • 类型名称大小写敏感,且不支持类型名称有小数点的类型。
  • 类型名称不支持同义词。
  • 支持类型名称格式为schema.package.type、package.type、schema.type、type,若未传入schema名,默认按当前schema处理,若其中的名称包含小数点,需要用双引号包裹。
  • 元素支持的基础类型为:int2、int4、int8、float4、float8、numeric、bool、bpchar、varchar、nvarchar2、name、text、timestamp、timestamptz、time、timetz、clob、bytea、blob。上述类型可能存在的别名,如:smallint、int、integer、bigint、number、float、boolean、char、varchar2等。
  • 元素支持的自定义类型:数组、集合和record类型。
  • JDBC开启URL参数enableGaussArrayAndStruct后,支持获取集合或数组类型的出参和返回值为Struct对象。支持出参还需要数据库额外开启GUC参数behavior_compat_options = 'proc_outparam_override'。
  • JDBC端不支持对类型修饰符进行校验,依赖于数据库校验,例如:
    1. 不支持元素精度转换,如record(c1 numeric(3, 1), c2 numeric(3, 1))类型,构造Struct时传入的列元素数值为2.55,JDBC端不会将其转换为2.6。
    2. 不支持元素长度校验,如record(c1 varchar(5), c2 varchar(5))类型,构造Struct时传入字符串长度大于5,JDBC端不报错。
    3. 不支持not null校验,如record(c1 int not null, c2 int)类型,构造Struct时第一列元素为null,JDBC端不报错。
  • 当传入数据库的struct对象包含空元素时,数据库会将这些空元素转换为NULL。例如:当元素类型为字符类型时,如果元素为空串,传给数据库时会转换为null;当元素类型为CLOB时,如果CLOB元素存储的字符串长度为0,传给数据库时会转换为null;当元素类型为BLOB时,如果BLOB元素存储的二进制数组长度为0,传给数据库时会转换为null;当元素类型为BYTEA时,如果BYTEA元素存储的二进制数组长度为0,传给数据库时会转换为null。

    不推荐在构造的struct对象中存储空元素,因为空元素在传给数据库时会转换为null。例如,元素为空串时,会转换为null,具体示例可参考上述示例3。

    当数据库传给客户端的struct对象包含空元素时,客户端也会将这些空元素转为null。

  • Struct接口的相关说明可参考GaussStruct对象支持的标准接口下方的说明。
  • StructDescriptor为非标准接口,StructDescriptor的getSQLType接口固定返回java.sql.Types.STRUCT。

    StructDescriptor的getSQLTypeName接口返回的类型名可参考数据类型映射关系

Struct标准接口参考:

方法名

返回值类型

throws

支持情况

getSQLTypeName()

String

SQLException

支持

getAttributes()

Object[]

SQLException

支持

getAttributes(java.util.Map<String,Class<?>> map)

Object[]

SQLException

不支持

说明:
  • getAttribute接口获取的对象类型为Object[],返回的数组中每个元素的类型可参考数据类型映射关系的JAVA变量类型列。

    例如:对于record(c1 int, c2 varchar(5))类型,getAttribute返回Object[]中第一个元素为null或者Integer类型数据,第二个元素为null或者String类型数据。

    特殊说明:元素类型为int2/smallint,元素存取为Short类型。

  • getSQLTypeName接口返回的类型名可参考数据类型映射关系,其中集合类型、数组类型、record类型的类型名映射规则如下:
    1. 若为package里定义的类型,类型名通常格式为schema.package.type。
      若schema、package、type中有任意名称不符合规则,则类型名格式为"schema"."package"."type":
      • 只能包含字母、数字或下划线。
      • 第一个字符不能是数字。
    2. 若为schema里定义的类型,类型名通常格式为schema.type。
      若schema、type中有任意名称不符合规则,则类型名格式为"schema"."type":
      • 只能包含字母、数字或下划线。
      • 第一个字符不能是数字。

我们使用cookie来确保您的高速浏览体验。继续浏览本站,即表示您同意我们使用cookie。 详情

文档反馈

文档反馈

意见反馈

0/500

标记内容

同时提交标记内容