ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sundar Sankar" <fatboys...@gmail.com>
Subject Re: Call stored procedure with reference to a table as the input param.
Date Fri, 11 Jan 2008 23:24:22 GMT
I found a way to insert a table of Strings. But I am still working on
inserting a table of objects. The code for inserting the table table of
String is

1. We have to use a type handler for this. Create a typeHandler like this

* * * ** ** ** * * ** * * ** ** *

*package* some.package*;*
*import* java.sql.Connection;
*import* java.sql.SQLException;
*import* java.sql.Statement;
*import* java.util.ArrayList;
*import* oracle.sql.ARRAY;
*import* oracle.sql.ArrayDescriptor;
*import* com.ibatis.sqlmap.client.extensions.ParameterSetter;
*import* com.ibatis.sqlmap.client.extensions.ResultGetter;
*import* com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

/**

* The Class StringArrayTypeHandler.

*/
* *

*public* *class* StringArrayTypeHandler *implements* TypeHandlerCallback {

/* (non-Javadoc)

* @see com.ibatis.sqlmap.client.extensions.TypeHandlerCallback#getResult(
com.ibatis.sqlmap.client.extensions.ResultGetter)

*/

*public* Object getResult(ResultGetter arg0) *throws* SQLException {

*return* *null*;

}

/* (non-Javadoc)

* @see com.ibatis.sqlmap.client.extensions.TypeHandlerCallback#setParameter(
com.ibatis.sqlmap.client.extensions.ParameterSetter, java.lang.Object)

*/

*public* *void* setParameter(ParameterSetter setter, Object parameter) *
throws* SQLException {

*if* (parameter *instanceof* ArrayList) {
ArrayList arr = (ArrayList)parameter;
Statement stmt = setter.getPreparedStatement();
Connection conn = stmt.getConnection();
ArrayDescriptor desc = ArrayDescriptor.*createDescriptor*("STRARRAY",
conn);// The STRARRAY is the name of the oracle type created as the
instance of
table.
parameter = *new* ARRAY(desc, conn,arr.toArray());
}

setter.setObject(parameter);
}

/* (non-Javadoc)

* @see com.ibatis.sqlmap.client.extensions.TypeHandlerCallback#valueOf(
java.lang.String)

*/

*public* Object valueOf(String arg0) {

*return* *null*;

}

}


2. Create the reference to the type handler in the sqlmapconfig.xml. Make
sure this is added before the resources.

    <typeHandler callback="some.package.StringArrayTypeHandler" javaType="
java.util.ArrayList" jdbcType="STRARRAY"/>

3. Make sure that the javatype and jdbctype in the sql-map file is the same
as the one given in the type handler. The class is auotmatically called,
once you have mentioned that.


Thats it you will be able to insert table / array of values into an oracle
stored procedure.


Regards

Sundar Sankarnarayanan

Mime
View raw message