< Prev - Examples - Next >

Scalar Types Returning A Transfer Object

Use Case

Fetch data from a single row.

Example Description

Stored Procedure

PL/SQL Procedure : SERVER_INFO
create or replace procedure server_info
(
  i_diff        in   number,
  o_date        out  date,
  o_timestamp   out  timestamp,
  o_instance    out  varchar2,
  o_db_version  out  number,
  o_db_release  out  number
)
/**
 * Database server information.
 *
 * @param i_diff Offset of date and timestamp value.
 * @param o_date System date plus i_diff offset.
 * @param o_timestamp System timestamp plus i_diff offset.
 * @param o_instance Instance information.
 * @param o_db_version Oracle version.
 * @param o_db_release Oracle release.
 */
is
begin
  o_date := sysdate + i_diff;
  o_timestamp := systimestamp - i_diff;
  select sys_context('USERENV','INSTANCE_NAME')
    into o_instance
    from dual;
  o_db_version := DBMS_DB_VERSION.VERSION;
  o_db_release := DBMS_DB_VERSION.RELEASE;
end server_info;

Generated Transfer Object

Java Transfer Object : ServerInfoTO.java
/*
 * This file is generated by PL/SQL Enterprise Workbench Connector Builder.
 * PL/SQL Enterprise Workbench Copyright (c) Jan Richter, Germany, Hamburg, 2015-2018. All rights reserved.
 *
 * THIS FILE IS NOT INTENDED TO BE MODIFIED - IT WILL BE OVERWRITTEN ON EVERY RUN OF THE CONNECTOR BUILDER
 */
package transferobject;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

/**
 * Transfer object returned from PL/SQL procedure <em>SERVER_INFO</em>.<p>
 */
final public class ServerInfoTO implements Serializable {
  private static final long serialVersionUID = 1L;

  /** System date plus iDiff offset. */ 
  public Date oDate;

  /** System timestamp plus iDiff offset. */ 
  public Timestamp oTimestamp;

  /** Instance information. */ 
  public String oInstance;

  /** Oracle version. */ 
  public int oDbVersion;

  /** Oracle release. */ 
  public int oDbRelease;
}

Factory API : Calling the PL/SQL procedure

Using the static factory to get the remote service.
Java Calling Class : ServerInfoFactoryApi.java
package plsql_workbench_examples.factoryapi;

import factory.ExamplesRPCFactory;
import service.ServerInfoService;
import transferobject.ServerInfoTO;

public class ServerInfoFactoryApi {
  public static void main(String[] args) {
    try {
      int diff = 10;
      
      // get the service
      ServerInfoService service = ExamplesRPCFactory.getServerInfoService();
      
      // calling the stored procedure, receiving a transfer object
      ServerInfoTO info = service.call(diff);

      // print server information
      System.out.println("database date(+" + diff + "):" + info.oDate);
      System.out.println("database timestamp(-" + diff + "):" + info.oTimestamp);
      System.out.println("database instance name:" + info.oInstance);
      System.out.println("database version:" + info.oDbVersion + "." + info.oDbRelease);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Spring API : Calling the PL/SQL procedure

Using Spring annotation to inject the service and call the remote service.
Java Calling Class : ServerInfoSpringApi.java
package plsql_workbench_examples.springapi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import service.ServerInfoService;
import transferobject.ServerInfoTO;

@Component
public class ServerInfoSpringApi {
  @Autowired
  private ServerInfoService serverInfoService;

  public static void main(String[] args) {
    // Register Spring Beans, Spring Context and call demo method 
    try (GenericApplicationContext ctx = BaseSpringConfig.getCtx(ServerInfoSpringApi.class)) {
      ctx.getBean(ServerInfoSpringApi.class).runDemo();
    }
  }

  private void runDemo() {
    try {
      int diff = 10;

      // calling the stored procedure, receiving a transfer object
      ServerInfoTO info = serverInfoService.call(diff);

      // print server information
      System.out.println("database date(+" + diff + "):" + info.getODate());
      System.out.println("database timestamp(-" + diff + "):" + info.getOTimestamp());
      System.out.println("database instance name:" + info.getOInstance());
      System.out.println("database version:" + info.getODbVersion() + "." + info.getODbRelease());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}