Assuming that the endgame of your "action", as implied in "When it comes to comparing arraylists or creating tables of the arraylists the data is not aligned because of the differing lengths", is to store data into database the answer is simple..
Batched Prepared Statement.. these beauties are perfect for doing massive amount of work that is repetitive in nature..
Simply create 5 (n) prepared statement and add batch as you see fit, giving the name of the element, the value of the element and a father key (e.g. the "id" of the relevant item element) and add the "prepared" insert to the batch of the relevant prepared statement.. than execute the whole lot and there you have it, alignement taken care of for free as well ...
A sample code of how i would envision this (long, but maybe it will give you some ideas..)... In the sample all prepared statement do the very same thing, but I'm giving four different "actor" method, so that you can basically customize the target query for each different "element" of the item type. Additionally you should plan a full rollback method, as with this "thing" you will commit each block (preparedstatement) when the cap of queryLimit (10000) is reached, so for rolling back you will need to do a manual method cleaning the relevant item (e.g. adding for example a "referral" id of the xml that originated the elements iteselves).
public class ExternalQueryInputAnnex {
private int queryLimit = 10000;
//prep statement for elementA
private PreparedStatement ps1;
private int cnt1 = 0;
//prep statement for elementB
private PreparedStatement ps2;
private int cnt2 = 0;
//prep statement for elementC
private PreparedStatement ps3;
private int cnt3 = 0;
//prep statement for elementD
private PreparedStatement ps4;
private int cnt4 = 0;
//prep statement for elementE
private PreparedStatement ps5;
private int cnt5 = 0;
private void theThingYouNeed(List<Object> itemList)
{
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element)list.item(i);
String nodeName = element.getNodeName();
switch (nodeName) {
case "A":
doPreparedStatementOne("element.getChildNodes().item(0).getNodeValue()", "A", "whatever");
break;
case "B":
doPreparedStatementTwo("element.getChildNodes().item(0).getNodeValue()", "B", "whatever");
break;
case "C":
doPreparedStatementThree("element.getChildNodes().item(0).getNodeValue()", "C", "whatever");
break;
case "D":
doPreparedStatementFour("element.getChildNodes().item(0).getNodeValue()", "D", "whatever");
break;
case "E":
//e.add(element.getChildNodes().item(0).getNodeValue());
doPreparedStatementFive("element.getChildNodes().item(0).getNodeValue()", "E", "whatever");
break;
}
}
}
private void doPreparedStatementFive(String attribute, String sonId, String fatherId) throws DatabaseException {
int i = 0;
String query = "INSERT INTO TRS_ORDER_PRICE_INT_DTL_TIMES "
+ "(SON_VALUE,SON_ID,FATHER_ID) "
+ "VALUES "
+ "(?,?,?)";
try {
if (ps5 == null)
{
ps5 = connect.prepareStatement(query);
}
ps5.setString(1, attribute);
ps5.setString(2, sonId);
ps5.setString(3, fatherId);
ps5.addBatch();
cnt5++;
if (cnt5 >= queryLimit)
{
ps5 = wrapperDoBulkQueryList(ps5, query);
cnt5 = 0;
}
}
} catch (SQLException e) {
logger.error("Error insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
throw new DatabaseException("Sql Exception during insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
}
}
private void doPreparedStatementFour(String attribute, String sonId, String fatherId) throws DatabaseException {
int i = 0;
String query = "INSERT INTO TRS_ORDER_PRICE_INT_DTL_TIMES "
+ "(SON_VALUE,SON_ID,FATHER_ID) "
+ "VALUES "
+ "(?,?,?)";
try {
if (ps4 == null)
{
ps4 = connect.prepareStatement(query);
}
ps4.setString(1, attribute);
ps4.setString(2, sonId);
ps4.setString(3, fatherId);
ps4.addBatch();
cnt4++;
if (cnt4 >= queryLimit)
{
ps4 = wrapperDoBulkQueryList(ps4, query);
cnt4 = 0;
}
}
} catch (SQLException e) {
logger.error("Error insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
throw new DatabaseException("Sql Exception during insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
}
}
private void doPreparedStatementThree(String attribute, String sonId, String fatherId) throws DatabaseException {
int i = 0;
String query = "INSERT INTO TRS_ORDER_PRICE_INT_DTL_TIMES "
+ "(SON_VALUE,SON_ID,FATHER_ID) "
+ "VALUES "
+ "(?,?,?)";
try {
if (ps3 == null)
{
ps3 = connect.prepareStatement(query);
}
ps3.setString(1, attribute);
ps3.setString(2, sonId);
ps3.setString(3, fatherId);
ps3.addBatch();
cnt3++;
if (cnt3 >= queryLimit)
{
ps3 = wrapperDoBulkQueryList(ps3, query);
cnt3 = 0;
}
}
} catch (SQLException e) {
logger.error("Error insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
throw new DatabaseException("Sql Exception during insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
}
}
private void doPreparedStatementTwo(String attribute, String sonId, String fatherId) throws DatabaseException {
int i = 0;
String query = "INSERT INTO TRS_ORDER_PRICE_INT_DTL_TIMES "
+ "(SON_VALUE,SON_ID,FATHER_ID) "
+ "VALUES "
+ "(?,?,?)";
try {
if (ps2 == null)
{
ps2 = connect.prepareStatement(query);
}
ps2.setString(1, attribute);
ps2.setString(2, sonId);
ps2.setString(3, fatherId);
ps2.addBatch();
cnt2++;
if (cnt2 >= queryLimit)
{
ps2 = wrapperDoBulkQueryList(ps2, query);
cnt2 = 0;
}
}
} catch (SQLException e) {
logger.error("Error insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
throw new DatabaseException("Sql Exception during insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
}
}
private void doPreparedStatementOne(String attribute, String sonId, String fatherId) throws DatabaseException {
int i = 0;
String query = "INSERT INTO TRS_ORDER_PRICE_INT_DTL_TIMES "
+ "(SON_VALUE,SON_ID,FATHER_ID) "
+ "VALUES "
+ "(?,?,?)";
try {
if (ps1 == null)
{
ps1 = connect.prepareStatement(query);
}
ps1.setString(1, attribute);
ps1.setString(2, sonId);
ps1.setString(3, fatherId);
ps1.addBatch();
cnt1++;
if (cnt1 >= queryLimit)
{
ps1 = wrapperDoBulkQueryList(ps1, query);
cnt1 = 0;
}
}
} catch (SQLException e) {
logger.error("Error insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
throw new DatabaseException("Sql Exception during insertOrderPriceIntDtlTimes: "+e.getLocalizedMessage());
}
}
// do any sub-querylimit execution
public void cleanUpExecution() throws DatabaseException
{
if (ps1 != null)
{
logger.debug("ps1");
wrapperDoBulkQueryList(ps1, null);
}
if (ps2 != null)
{
logger.debug("ps2");
wrapperDoBulkQueryList(ps2, null);
}
if (ps3 != null)
{
logger.debug("ps3");
wrapperDoBulkQueryList(ps3, null);
}
if (ps4 != null)
{
logger.debug("ps4");
wrapperDoBulkQueryList(ps4, null);
}
if (ps5 != null)
{
logger.debug("ps5");
wrapperDoBulkQueryList(ps5, null);
}
}
public PreparedStatement wrapperDoBulkQueryList(PreparedStatement queryBulk, String query) throws DatabaseException
{
queryBulk = wrapperDoBulkQueryListDirect(queryBulk, null, query);
return queryBulk;
}
public PreparedStatement wrapperDoBulkQueryListDirect(PreparedStatement queryList, Long load_id, String query) throws DatabaseException
{
if (doBulkQueryList(queryList))
{
if (query != null)
{
try
{
queryList = connect.prepareStatement(query);
return queryList;
}
catch (SQLException e)
{
throw new DatabaseException("Exception of sqlexception while regenerating statement:"+e.getLocalizedMessage());
}
}
else
{
logger.debug("Query not passed, cleanup for preparedstatement");
//System.out.println("Query not passed, cleanup for preparedstatement");
return queryList;
}
}
else
{
// do delete based on loadid
// then throw exception
throw new DatabaseException("Some failure");
}
}
public boolean doBulkQueryList(PreparedStatement queryList) throws DatabaseException
{
// do bulk query execution
try {
connect.setAutoCommit(false);
//System.out.println("query: "+singleQuery.toString());
long startSetup = System.currentTimeMillis();
int[] results = queryList.executeBatch();
long finishSetup = (System.currentTimeMillis()-startSetup)/1000;
if(finishSetup > 5){
logger.warn("Execution "+results.length+" insert in "+finishSetup+" seconds.");
}
for(int singleResult : results){
if (singleResult >= 0) {
// System.out.println("OK; updateCount");
}
else if (singleResult == Statement.SUCCESS_NO_INFO) {
// System.out.println("OK; insert=Statement.SUCCESS_NO_INFO");
}
else if (singleResult == Statement.EXECUTE_FAILED) {
System.out.println("Failure; updateCount=Statement.EXECUTE_FAILED");
}
}
insertedRows = insertedRows + results.length;
//System.out.println("Query effettuata");
connect.commit();
} catch (SQLException e) {
//e.printStackTrace();
if (connect != null) {
try {
logger.error("Transaction is being rolled back: ",e);
connect.rollback();
return false;
} catch(SQLException excep) {
throw new DatabaseException("Error Transaction commit");
}
}
}
finally{
if (queryList != null) {
try {
queryList.close();
} catch (SQLException e) {
logger.error("Transaction is being rolled back: "+e.getLocalizedMessage());
}
}
try {
connect.setAutoCommit(true);
} catch (SQLException e) {
logger.error("Set true auto commit: ",e);
}
}
return true;
}
}
HashSetinstead to avoid duplication