Getting an OBJECT Value from an Oracle Table - Java JDBC

Java examples for JDBC:Oracle

Description

Getting an OBJECT Value from an Oracle Table

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) {
    try {
      Connection connection = null;
      // Create a statement
      Statement stmt = connection.createStatement();

      // Select rows from object1_table
      ResultSet resultSet = stmt.executeQuery("SELECT * FROM object1_table");

      // Get the OBJECT values from each row
      while (resultSet.next()) {
        // Get the integer from the first column col_integer of the row
        int i = resultSet.getInt(1);

        // Get the object1 value from the second column col_object1
        oracle.sql.STRUCT object1 = (oracle.sql.STRUCT) resultSet.getObject(2);

        // Get the object1 values from each row
        Object[] object1Values = object1.getAttributes();

        // Get the first value of object1, which is a string
        String str = (String) object1Values[0];

        // Get the second value of object1, which is of the type object2
        oracle.sql.STRUCT object2 = (oracle.sql.STRUCT) object1Values[1];

        // Get the values of object2
        Object object2Values[] = object2.getAttributes();
        str = (String) object2Values[0];
        BigDecimal num = (BigDecimal) object2Values[1];
      }
    } catch (SQLException e) {
    }
  }
}

Related Tutorials