Sunday, February 26, 2017

Convert java List to oracle.sql.ARRAY using org.springframework.jdbc.core.JdbcTemplate

This class will convert the java List of objects to sql ARRAY objects which are used to pass to a data base procedure.


import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;

import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;


/**
 * Arun G @author
 *
 */
public class OracleTypeHandler {

/**
* This method returns Oracle.sql.Type of Object
*
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public static ARRAY returnOracleType(final List<?> dataList, final String typeObject,
     javax.sql.DataSource dataSource) throws ClassNotFoundException,
     SecurityException, NoSuchFieldException, IllegalArgumentException,
     IllegalAccessException, SQLException {

JdbcTemplate jdbc = new JdbcTemplate(dataSource);

ARRAY array = (ARRAY) jdbc.execute(new ConnectionCallback<Object>(){

@Override
public Object doInConnection(Connection con) throws SQLException {
ARRAY array = null;
Object[] objectArray = null;
if (null != dataList && !dataList.isEmpty()) {
objectArray = new Object[dataList.size()];

for (int i = 0; i < objectArray.length; i++) {
Object copyObject = dataList.get(i);
Class<?> classObject = copyObject.getClass();
Field[] fields = classObject.getDeclaredFields();
Object[] attributes = new Object[fields.length-1];
for (int j = 0; j < fields.length-1; j++) {

try {
attributes[j] = getInstanceValue(copyObject, fields[j].getName());
} catch (Exception e) {

throw new Exception(e);
}


}
objectArray[i] = attributes;

}

ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(typeObject,
con.unwrap(oracle.jdbc.OracleConnection.class));

array = new ARRAY(descriptor, con.unwrap(oracle.jdbc.OracleConnection.class), objectArray);
}else{
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(typeObject,
con.unwrap(oracle.jdbc.OracleConnection.class));
array = new ARRAY(descriptor, con.unwrap(oracle.jdbc.OracleConnection.class), null);
}

return array;


}// end of doInConnection

}); // end of ConnectionCallback
return array;

}

/**
* Returns an object containing the value of any field of an object instance
* (even private).
*
* @param classInstance
*           An Object instance.
* @param fieldName
*           The name of a field in the class instantiated by classInstance
* @return An Object containing the field value.
* @throws SecurityException .
* @throws NoSuchFieldException .
* @throws ClassNotFoundException .
* @throws IllegalArgumentException .
* @throws IllegalAccessException .
*/
public static Object getInstanceValue(final Object classInstance,
     final String fieldName) throws SecurityException,
     NoSuchFieldException, ClassNotFoundException,
     IllegalArgumentException, IllegalAccessException {
// Get the private field
final Field field = classInstance.getClass().getDeclaredField(fieldName);
// Allow modification on the field
field.setAccessible(true);
// Return the Obect corresponding to the field
return field.get(classInstance);
}



}
public CLOB convertToClob(final String data) throws SQLException {

JdbcTemplate jdbc = new JdbcTemplate(getBbctDataSource());

CLOB clob = (CLOB) jdbc.execute(new ConnectionCallback<Object>() {

@Override
public Object doInConnection(Connection con) throws SQLException {

CLOB clob = (CLOB) con.createClob();
clob.setString(1, data);
return clob;

}
});
return clob;
}