Use Oracle’s VARRAY Through JDBC

This code snippet demonstrates how to use Oracle’s VARRAY through JDBC. The code:

  1. Creates VARRAYs of different lengths.
  2. Contains a table to store data of above array type.
  3. Stores the objects in an array.
  4. Inserts them into database table in two different ways (with SQL and ArrayDescriptor).
import java.sql.*;import oracle.sql.*;import oracle.jdbc.oracore.Util;import oracle.jdbc.*;public class VArrayManipulation{  public static void main (String args[])    throws Exception  {    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());    // The sample retrieves an varray of type "STRING_VARRAY",    // materializes the object as an object of type ARRAY.    // A new ARRAY is then inserted into the database.    String url = "";    // Connect to the database    Connection conn =      DriverManager.getConnection (url, "" , "");    conn.setAutoCommit (false);    // Create a Statement    Statement stmt = conn.createStatement ();    try    {      stmt.execute ("DROP TABLE sample_varray_table");      stmt.execute ("DROP TYPE string_varray");    }    catch (SQLException e)    {        //Exceptions will be thrown if Table and types doesnt exist . Ignore this    }    stmt.execute ("CREATE TYPE string_varray AS VARRAY(10) OF VARCHAR2(100)");    stmt.execute ("CREATE TABLE sample_varray_table (acol string_varray)");    //Insert using SQL    stmt.execute ("INSERT INTO sample_varray_table VALUES (string_varray('Test1', 'Test2'))");    ResultSet rs = stmt.executeQuery("SELECT acol FROM sample_varray_table");    printResultSet (rs);    //Insert using ArrayDescriptor    // create a new ARRAY object    String arrayElements[] = { "Test3", "Test4" };    ArrayDescriptor desc = ArrayDescriptor.createDescriptor                                           ("STRING_VARRAY", conn);    ARRAY newArray = new ARRAY(desc, conn, arrayElements);    PreparedStatement ps =      conn.prepareStatement ("insert into sample_varray_table values (?)");    ((OraclePreparedStatement)ps).setARRAY (1, newArray);    ps.execute ();    rs = stmt.executeQuery("SELECT acol FROM sample_varray_table");    printResultSet (rs);    // Close all the resources    rs.close();    ps.close();    stmt.close();    conn.close();  }  public static void printResultSet (ResultSet rs)    throws SQLException  {    System.out.println("********Fetch Starts....********");    int line = 0;    while (    {      line++;      System.out.println("********Row "+line+" : ");      ARRAY array = ((OracleResultSet)rs).getARRAY (1);      System.out.println ("Array is of type "+array.getSQLTypeName());      System.out.println ("Array is of length "+array.length());      // get Array elements      String[] values = (String[]) array.getArray();      for (int i=0; i

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.