Use Oracle’s VARRAY Through JDBC

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as