Pass and return custom array object in ibatis and oracle in java

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I've looked around for a good example of this, but I haven't run into one yet. I want to pass a custom string array from java to oracle and back, using the IBATIS framework. Does anyone have a good link to an example? I'm calling stored procs from IBATIS.

Thanks

回答1:

You've got to start with a custom instance of TypeHandler. We'd prefer to implement the simpler TypeHandlerCallback, but in this scenario we need access to the underlying Connection.

public class ArrayTypeHandler implements TypeHandler {      public void setParameter(PreparedStatement ps, int i, Object param, String jdbcType)             throws SQLException {         if (param == null) {             ps.setNull(i, Types.ARRAY);         } else {             Connection conn = ps.getConnection();             Array loc = conn.createArrayOf("myArrayType", (Object[]) param);             ps.setArray(i, loc);         }     }      public Object getResult(CallableStatement statement, int i)             throws SQLException {         return statement.getArray(i).getArray();     }     ... }

Then, to wire it up in the iBATIS config:

                                               {? = call My_Array_Function( ? )}       

Hope this helps!



回答2:

bsanders gave me a good starting point - here's what I had to do to make it work within the RAD environment (websphere 6.2).

public Object getResult(CallableStatement statement, int i) throws SQLException {     return statement.getArray(i).getArray(); //getting null pointer exception here }  public void setParameter(PreparedStatement ps, int i, Object param, String jdbcType) throws SQLException {     if (param == null) {         ps.setNull(i, Types.ARRAY);      } else {         String[] a = (String[]) param;         //ARRAY aOracle = ARRAY.toARRAY(a, (OracleConnection)ps.getConnection());          //com.ibm.ws.rsadapter.jdbc.WSJdbcConnection         w = (com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)ps.getConnection());          //com.ibm.ws.rsadapter.jdbc.WSJdbcObject x;         Connection nativeConnection = Connection)WSJdbcUtil.getNativeConnection((WSJdbcConnection)ps.getConnection());          ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("F2_LIST", nativeConnection);         ARRAY dataArray = new ARRAY(descriptor, nativeConnection, a);         ps.setArray(i, dataArray);     } }

Notice the nativeConnection I had to get, the descriptor I had to make, and so on. However, while I can pass things into the database as an array of Strings, I haven't been able to figure out why I'm not getting anything back. My OUT parameter (the getResult(CallableStatement statment, int i) is throwing a null pointer exception, even though I'm setting the out parameter in the plsql in the database.

--stored procedure to take a | delimited ids    PROCEDURE array_test (argument IN f2_list, result OUT f2_list)     AS       l_procname_v   VARCHAR2 (50)                 := 'array_test';       l_param_list   VARCHAR2 (2000)                    := l_procname_v || ' param_values: p_string: ';        p_status_n     NUMBER;       p_message_v    VARCHAR2 (2000);       ret_list f2_list := new f2_list
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!