OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Connector/J

V2.2.3

  • What is OceanBase Connector J?
    • JDBC overview
    • Compatibility requirements
  • Instructions
    • Install OceanBase Client
    • Basic operations
      • Import packages
      • Create a database connection
      • Create a Statement object
      • Run queries and retrieve ResultSet objects
      • Process a ResultSet object
      • Close ResultSet and Statement objects
      • Modify a database
      • Commit changes
      • Close a database connection
    • Handle SQL exceptions
    • Java data streams
      • Overview of Java data streams
      • LONG and LONG RAW data types
      • LOB data types
    • Use LOBs
      • Overview of LOB data types
      • LOB data API
      • LOB locator API
      • CHAR, VARCHAR, and RAW data types
    • Use CallableStatements to call stored procedures
    • Data sources and URL
      • Data source overview
      • Data source characteristics and properties
      • Create and connect to a data source instance
      • Database URL
    • Result set
      • Limits and degradation rules
      • Fetch size
      • Refetch rows
      • Cursor result sets
    • Statement caching
      • Statement caching overview
      • Use statement cache
      • Reuse statement objects
    • Failover and load balancing modes
    • Batch processing
    • JDBC security features
    • SQL NetworkCostInfo
    • Troubleshooting
    • Common APIs
      • java.sql.Connection
      • java.sql.CallableStatement
      • java.sql.DatabaseMetaData
      • java.sql.Driver
      • java.sql.PreparedStatement
      • java.sql.ParameterMetaData
      • java.sql.ResultSet
      • java.sql.ResultSetMetaData
      • java.sql.Statement
      • javax.sql.ConnectionPoolDataSource
      • javax.naming.Context
      • javax.sql.PooledConnection
    • Reference
      • Supported SQL and PL data types
  • Features specific to the Oracle mode
    • Call PL stored procedures
    • Result set performance
    • Use ARRAY and STRUCT classes
    • Distributed transactions
      • Overview of distributed transactions
      • XA components
      • Distributed transaction examples
    • Error messages in Oracle mode
    • Data types supported in Oracle mode

Download PDF

JDBC overview Compatibility requirements Install OceanBase Client Import packages Create a database connection Create a Statement object Run queries and retrieve ResultSet objects Process a ResultSet object Close ResultSet and Statement objects Modify a database Commit changes Close a database connection Handle SQL exceptions Overview of Java data streams LONG and LONG RAW data types LOB data types Overview of LOB data types LOB data API LOB locator API CHAR, VARCHAR, and RAW data types Use CallableStatements to call stored procedures Data source overview Data source characteristics and properties Create and connect to a data source instance Database URL Limits and degradation rules Fetch size Refetch rows Cursor result sets Statement caching overview Use statement cache Reuse statement objects Failover and load balancing modesBatch processing JDBC security features SQL NetworkCostInfo Troubleshooting java.sql.Connection java.sql.CallableStatement java.sql.DatabaseMetaData java.sql.Driver java.sql.PreparedStatement java.sql.ParameterMetaData java.sql.ResultSet java.sql.ResultSetMetaData java.sql.Statement javax.sql.ConnectionPoolDataSource javax.naming.Context javax.sql.PooledConnection Supported SQL and PL data types Call PL stored procedures Result set performance Use ARRAY and STRUCT classes Overview of distributed transactions XA components Distributed transaction examples Error messages in Oracle mode Data types supported in Oracle mode
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Connector/J
  3. V2.2.3
iconOceanBase Connector/J
V 2.2.3
  • V 2.4.17
  • V 2.4.16
  • V 2.4.15
  • V 2.4.14
  • V 2.4.5
  • V 2.4.4
  • V 2.4.3
  • V 2.4.2
  • V 2.4.1
  • V 2.4.0
  • V 2.2.11
  • V 2.2.10
  • V 2.2.7
  • V 2.2.6
  • V 2.2.3
  • V 2.2.0

Use ARRAY and STRUCT classes

Last Updated:2026-04-09 07:36:39  Updated
share
What is on this page
ARRAY descriptor
ARRAY class methods
Examples of using the ARRAY and STRUCT classes

folded

share

ARRAY and STRUCT classes belong to the same collection class and are usually used together.

ARRAY descriptor

To create and use an ARRAY object, you need to specify a descriptor, that is, an instance of the oracle.sql.ArrayDescriptor class. For any number of ARRAY objects corresponding to the same SQL type, only one ArrayDescriptor object is required.

ARRAY class methods

The oracle.sql.ARRAY class offers the following methods:

  • getDescriptor

    This method returns an ArrayDescriptor object that describes the array type.

  • getArray

    This method retrieves the content of an array of the default JDBC type. If it retrieves an array of objects, getArray determines the type by using the default type mapping of database connection objects.

  • getOracleArray

    This method is the same as getArray, except that it retrieves elements in the oracle.sql* format.

  • getBaseType

    This method returns the SQL type code of the array elements.

  • getBaseTypeName

    This method returns the SQL type name of the array elements.

  • getSQLTypeName

    This method returns the standard SQL type name for the entire array. This method is an Oracle extension.

  • getResultSet

    This method materializes array elements into a result set.

  • getJavaSQLConnection

    This method returns the connection instance associated with this array.

  • length

    This method returns the number of elements in the array.

Examples of using the ARRAY and STRUCT classes

 @BeforeClass()
    public static void initClass() throws SQLException {
        createTable(tablenameArray, "c1 int primary key,c2 interval day(6) to second(5)");
        createTable(tablenameStruct, " c1 int primary key,c2 interval year(4) to month");
        createTable(array1, "c1 int");
        createTable(array2, "c1 char(100)");
        createTable(array3, "c1 date");
        createTable(struct1, "c1 int");
        createTable(struct2, "c1 varchar(100)");
        createTable(struct3, "c1 date");
        createTable(raw1, "c1 int ,c2 raw(100)");
        createTable(refcursor1, "c1 varchar(20), c2 number");
    }

    public void showArrayRes(Array array) throws Exception {
        ResultSet arrayRes = array.getResultSet();
        while (arrayRes.next()) {
            int index = arrayRes.getInt(1);
            Struct struct = (Struct) arrayRes.getObject(2);
            Object[] objArr = struct.getAttributes();
            for (Object obj : objArr) {
                System.out.printf("index j %d obj now is %s\n", index, obj);
            }
        }
    }

    public void executeSqls(String[] sqls, Connection conn) {
        PreparedStatement ps;
        for (String sql : sqls) {
            try {
                ps = conn.prepareStatement(sql);
                ps.execute();
                ps.close();
            } catch (Exception e) {
                System.out.println("sql:" + sql);
                // ignore, maybe table does not exist
                e.printStackTrace();
            }
        }
    }

 // Example 1
    public void basicArrayAndStructTest() throws SQLException {
        Assume.assumeTrue(sharedUsePrepare());
        Connection conn = null;
        try {
            conn = sharedPSConnection;
            PreparedStatement ps = null;
            Statement stmt = conn.createStatement();
            stmt.execute("CREATE OR REPLACE TYPE my_obj as object (c1 int, c3 date)");
            stmt.execute("CREATE OR REPLACE TYPE obj_array IS TABLE OF my_obj");

            // test input
            String createPlSql = "CREATE OR REPLACE PROCEDURE my_proc_objarr(X IN obj_array) IS "
                                 + "BEGIN " + "  FOR idx IN 1..X.count LOOP " + "    INSERT INTO "
                                 + array1 + " VALUES(X(idx).c1);"
                                 + "  END LOOP; "
                                 //                                 + "  FOR idx IN 1..X.count LOOP" + "    INSERT INTO " + array2
                                 //                                 + "  VALUES(X(idx).c2);" + "  END LOOP; "
                                 + "    FOR idx IN 1..X.count LOOP" + "    INSERT INTO " + array3
                                 + " VALUES(X(idx).c3);" + "  END LOOP; " + "END;";
            stmt.execute(createPlSql);
            Integer[] intArray = new Integer[10];
            for (int i = 0; i < 10; ++i) {
                if (i % 2 == 0) {
                    intArray[i] = null;
                } else {
                    intArray[i] = i;
                }
            }
            //            String[] strArray = new String[10];
            //            for (int i = 0; i < 10; ++i) {
            //                if (i % 2 == 0) {
            //                    strArray[i] = "luyun_" + i;
            //                } else {
            //                    strArray[i] = null;
            //                }
            //            }

            java.sql.Timestamp[] dateArray = new Timestamp[10];
            for (int i = 0; i < 10; ++i) {
                if (i % 2 == 0) {
                    dateArray[i] = Timestamp.valueOf("2019-06-04 10:29:11.123456");
                } else {
                    dateArray[i] = null;
                }
            }
            Object[] structArray = new Object[10];
            for (int i = 0; i < 10; ++i) {
                structArray[i] = conn.createStruct("my_obj", new Object[] { intArray[i],
                        dateArray[i] });
                //                        strArray[i], dateArray[i] });
            }
            Array array = conn.createArrayOf("my_obj", structArray);
            //            ((ArrayImpl)array).getComplexType().setTypeName("obj_array");
            {
                System.out.println("=======prepareStatement input test =============");
                ps = conn.prepareStatement("call my_proc_objarr(?)");
                ps.setArray(1, array);
                ps.execute();
                ps.close();

                ps = conn.prepareStatement("select * from " + array1);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getInt("c1"));
                }
                ps.close();

                //                ps = conn.prepareStatement("select * from " + array2);
                //                rs = ps.executeQuery();
                //                while (rs.next()) {
                //                    System.out.println(rs.getString("c1"));
                //                }
                //                ps.close();

                ps = conn.prepareStatement("select * from " + array3);
                rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getDate("c1"));
                }
                ps.close();
            }
            {
                System.out.println("=======callable Statement input test =============");
                ps = conn.prepareCall("call my_proc_objarr(?)");
                ps.setArray(1, array);
                ps.execute();
                ps.close();

                ps = conn.prepareStatement("select * from " + array1);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getInt("c1"));
                }
                ps.close();

                //                ps = conn.prepareStatement("select * from " + array2);
                //                rs = ps.executeQuery();
                //                while (rs.next()) {
                //                    System.out.println(rs.getString("c1"));
                //                }
                //                ps.close();

                ps = conn.prepareStatement("select * from " + array3);
                rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getDate("c1"));
                }
                ps.close();
            }
            // test output
            createPlSql = "CREATE OR REPLACE PROCEDURE my_proc_obj_out(x OUT obj_array) IS"
                          + " i int :=1;" + " BEGIN" + " x.extend(10);" + "  for idx in 1..5 loop"
                          //                          + "    x(i).c1 := idx;" + "    x(i).c2 := idx;"
                          + "    x(i).c1 := idx;" + "    x(i).c3 := date '1970-01-01';"
                          + "    x(i + 1).c1 := null;"
                          //                          + "    x(i + 1).c2 := null;" + "    x(i + 1).c3 := null;"
                          + "    x(i + 1).c3 := null;" + "    i := i + 2;" + "  end loop;" + "END;";
            {
                stmt.execute(createPlSql);
                ps = conn.prepareStatement("call my_proc_obj_out(?)");
                ps.setArray(1, null);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    Array resArray = rs.getArray(1);
                    showArrayRes(resArray);
                }
            }
            // callable test
            {
                System.out.println("=========callable stmt test==========");
                CallableStatement csmt = conn.prepareCall("{call my_proc_obj_out(?)}");
                csmt.registerOutParameter(1, Types.ARRAY, "obj_array");
                csmt.execute();
                Array resArray = csmt.getArray(1);
                showArrayRes(resArray);
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

  // Example 2
    public void basicRawTest() throws SQLException {
        Connection conn = null;
        try {
            conn = sharedPSConnection;
            PreparedStatement ps = null;
            ps = conn.prepareStatement("insert into " + raw1 + " values(?,?)");
            ps.setInt(1, 11);
            ps.setBytes(2, new byte[] { 1, 2, 3 });
            ps.execute();

            ps = conn.prepareStatement("select c1 ,c2 from " + raw1 + " where c1 = 11");
            ResultSet rs = ps.executeQuery();
            //            Assert.assertEquals("[B", rs.getMetaData().getColumnClassName(2));
            Assert.assertEquals("RAW", rs.getMetaData().getColumnTypeName(2));
            Assert.assertEquals(-3, rs.getMetaData().getColumnType(2));
            Assert.assertEquals(2, rs.getMetaData().getColumnCount());
            Assert.assertEquals("C2", rs.getMetaData().getColumnLabel(2));
            Assert.assertEquals(100, rs.getMetaData().getColumnDisplaySize(2));
            Assert.assertEquals("UNITTESTS", rs.getMetaData().getCatalogName(2));
            Assert.assertEquals("C2", rs.getMetaData().getColumnName(2));
            Assert.assertEquals(0, rs.getMetaData().getScale(2));
            Assert.assertEquals("TEST_RAW1", rs.getMetaData().getTableName(2));
            //            assertFalse(rs.getMetaData().isCaseSensitive(2));
            assertTrue(rs.next());
            Assert.assertEquals(11, rs.getInt(1));
            Assert.assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, rs.getBytes(2)));
            Assert.assertEquals("010203", rs.getString(2));
            Assert.assertEquals(3, rs.getBinaryStream(2).available());
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }

   // Example 3
    public void testRefCursor() {
        for (int loop = 0; loop < 2; loop++) {
            Connection conn = sharedPSConnection;
            {
                {
                    String sql = "insert into " + refcursor1 + " values(?, ?)";
                    for (int i = 0; i < 10; i++) {
                        try {
                            PreparedStatement statement = conn.prepareStatement(sql);
                            statement.setString(1, "test" + i);
                            statement.setInt(2, i);
                            statement.execute();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    String sql = "select * from " + refcursor1;
                    PreparedStatement statement = conn.prepareStatement(sql);
                    statement.execute();
                    ResultSet resultSet = statement.getResultSet();
                    while (resultSet.next()) {
                        int columnCnt = resultSet.getMetaData().getColumnCount();
                        for (int j = 1; j <= columnCnt; j++) {
                            System.out.println(resultSet.getMetaData().getColumnName(j) + ":"
                                               + resultSet.getString(j));
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    Statement statement = conn.createStatement();
                    String createPlSql = "CREATE OR REPLACE PROCEDURE test_cursor(p_cursor OUT sys_refcursor) "
                                         + "is BEGIN "
                                         + " open p_cursor for select * from "
                                         + refcursor1 + ";" + "end;";
                    statement.execute(createPlSql);
                    CallableStatement csmt = conn.prepareCall("call test_cursor(?)",
                        ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
                    csmt.setFetchSize(10);
                    csmt.registerOutParameter(1, Types.REF);
                    csmt.execute();
                    ResultSet resultSet = (ResultSet) csmt.getObject(1);
                    resultSet.setFetchSize(2);
                    System.out.println("=========refcursor output==========");
                    while (resultSet.next()) {
                        int columnCnt = resultSet.getMetaData().getColumnCount();
                        for (int j = 1; j <= columnCnt; j++) {
                            System.out.println(resultSet.getMetaData().getColumnName(j) + ":"
                                               + resultSet.getString(j));
                        }
                    }
                    resultSet.close();
                    resultSet = (ResultSet) csmt.getObject(1);
                    System.out.println("=========refcursor output==========");
                    try {
                        while (resultSet.next()) {
                            int columnCnt = resultSet.getMetaData().getColumnCount();
                            for (int j = 1; j <= columnCnt; j++) {
                                System.out.println(resultSet.getMetaData().getColumnName(j) + ":"
                                                   + resultSet.getString(j));
                            }
                        }
                    } catch (SQLException e) {
                        Assert.assertEquals(e.getErrorCode(), 600);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    Assert.assertEquals(e.getMessage(), "cursor is not open");
                }
            }
        }
    }

Previous topic

Result set performance
Last

Next topic

Overview of distributed transactions
Next
What is on this page
ARRAY descriptor
ARRAY class methods
Examples of using the ARRAY and STRUCT classes