これは、次の場所に投稿された質問の続きです: Java program to pass List of Bean to a oracle stored procedure - オブジェクトを次々に追加するのではなく、リスト全体を一度に渡す
上記のリンクの場所に記載されているストアド プロシージャを拡張しようとしていますが、実装に混乱しています。プロシージャからの出力として VARCHAR2 ではなく、プロシージャからの出力として NUM_ARRAY を返したいと考えています。Java コードで NUM_ARRAY を読み取るロジックを実装するのを手伝ってくれませんか。通常、出力は Map out = super.execute(inParams); を使用して返されます。NUM_ARRAY を Bean に抽出するにはどうすればよいですか?
The source code implementation is as follows.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import oracle.jdbc.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.object.StoredProcedure;
public class RevPrdBrkDwnSP extends StoredProcedure{
private final Logger log = Logger.getLogger(this.getClass().getName());
public RevPrdBrkDwnSP(DataSource dataSource, String storeProcName) {
// Run the Parent
super(dataSource, storeProcName);
// Declare the Parameter Details
declareParameter(new SqlParameter("IN_ARRAY", OracleTypes.ARRAY, "****.PROD_PRCT_BRKDWN_TYPE_ARRAY"));
declareParameter(new SqlOutParameter("OUT_ARRAY", OracleTypes.ARRAY, "****.PROD_PRCT_BRKDWN_TYPE_ARRAY"));
// Compile the SP
compile();
}
public boolean execute(final RevAppViewBean appViewBean$Session, final DataSource dataSource) throws Exception {
boolean returnVal = false;
Map<String, Object> inParams = new HashMap<String, Object>();
log.info("Setting up the Store Procedure Params");
inParams.put("IN_ARRAY", new SqlTypeValue() {
public void setTypeValue(PreparedStatement cs, int index, int sqlType, String typeName) throws SQLException {
Connection con = cs.getConnection();
ArrayDescriptor des = ArrayDescriptor.createDescriptor("****.PROD_PRCT_BRKDWN_TYPE_ARRAY", con);
ARRAY a = new ARRAY(des, con, appViewBean$Session.getExcelRecLst().toArray());
cs.setObject(1, (Object)a);
}
});
inParams.put("OUT_ARRAY", identifier); // what should the identifier be ?????????
if (log.isDebugEnabled()) {
log.debug("Executing the **** Store Procedure ");
}
Map out = super.execute(inParams); // how to get the same array as value ??????
log.info("output size is --------------------->>>>>>>>>> "+out.size());
for(Object o : out.keySet()){
log.info((String)out.get(o));
returnVal = Boolean.parseBoolean((String)out.get(o));
}
if (log.isDebugEnabled()) {
log.info("Output from **** Store Procedure :" + out);
}
return returnVal;
}
}
更新: Spring Data JDBC 拡張機能を使用した後、以下に貼り付けた新しい応答に対応するためにソース コードを変更する必要がありましたが、bean.getAttributes() メソッドが呼び出されたときに接続の問題が依然として存在します。接続を閉じたり、接続が閉じられる前に値にアクセスしたりしないようにする方法を見つける必要があるようです。
Map out = super.execute(inParams);
log.info("output size is --------------------->>>>>>>>>> "+out.size()); //prints the actual value
Object[] idOutArraz = (Object[])out.get("OUT_ARRAY");
log.info("size of returnValue is "+idOutArraz.length); //prints the right number of results
for(int i= 0; i<idOutArraz.length;i++){
Object[] attrs = null;
Struct bean = (Struct) idOutArraz[i];
attrs = bean.getAttributes();
if (attrs != null) {
System.out.println(Arrays.asList(attrs));
}
}