I created five classes -> GeneralData, MaterialData, WeigthsAndDimensions, TechnicalData, LogisticData. All classes have a few attributes. I also created a class Material, whose constructor has five Object attributes:
package com.mmdmanager.others;
public class Material {
private GeneralData generalData;
private MaterialData materialData;
private WeigthsAndDimensions weigthsAndDimensions;
private TechnicalData technicalData;
private LogisticData logisticData;
public Material(GeneralData generalData, MaterialData materialData, WeigthsAndDimensions weigthsAndDimensions, TechnicalData technicalData, LogisticData logisticData) {
this.generalData = generalData;
this.materialData = materialData;
this.weigthsAndDimensions = weigthsAndDimensions;
this.technicalData = technicalData;
this.logisticData = logisticData;
}
@Override
public String toString() {
return generalData + "," + materialData + "," + weigthsAndDimensions + "," + technicalData + "," + logisticData;
}
}
My application create over one object of class "Material" and put all these objects into arraylist:
Material material = new Material(generalData, materialData, weigthsAndDimensions, technicalData, logisticData);
materialList.add(material);
I have to force an algorithm, which iterates through each attribute of all five classes included in an object "material". Also, if an arraylist materialList has more than one objects, the algorithm should iterate through each object. I created such a code:
public ArrayList<Material> getMaterialList(ArrayList<Material> materialList, GeneralData generalData, MaterialData materialData, WeigthsAndDimensions weigthsAndDimensions, TechnicalData technicalData, LogisticData logisticData) {
try {
connection = ConnectionProvider.getConnection();
while(!connection.isClosed()) {
preparedStatement = connection.prepareStatement("insert into materials(material_name,product_number,user_id,request_datetime,esk_number,request_type,request_sub_type,remark,batch_number, product_hierarchy, gross_Weight, net_Weight, material_Length, material_Width, material_Height, material_Volume, Capacity_Unit_Of_Measure, inverter, POWER_SUPPLY, CEMARK, REFR_APPLICATION, REFR_MODE, REFRIGERANT_TYPE, REFRIGERANT_WEIGHT, FREQUENCY, COMPRESSOR_TYPE, PACKAGING_STYLE, SALES_OEM_PRODUCT, BUY_OEM_PRODUCT, INDOOR_OUTDOOR, DG_INDICATOR_PROFILE, SALES_BRAND, BUSINESS_PILAR, MATERIAL_SOURCE, FACTORY_NAME, DESTINATION_MARKET) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
connection.setAutoCommit(false);
iterator = materialList.iterator();
while (iterator.hasNext()) {
for (counter = 1; counter < 37; counter++) {
if ((counter > 0 && counter < 4) || (counter > 5 && counter < 11) || (counter == 32) || ((counter > 33 && counter < 37))) {
preparedStatement.setString(counter,String.valueOf(iterator.next()));
}
else if (counter == 4) {
preparedStatement.setTimestamp(counter, Timestamp.valueOf(String.valueOf(iterator.next())));
}
else if (counter == 5) {
preparedStatement.setInt(counter,Integer.valueOf(String.valueOf(iterator.next())));
}
else if ((counter > 10 && counter < 17) || (counter > 24 && counter <25)) {
preparedStatement.setDouble(counter, Double.valueOf(String.valueOf(iterator.next())));
}
else if ((counter > 16 && counter < 24) || (counter > 25 && counter < 32) || counter == 33) {
preparedStatement.setDouble(counter, Byte.valueOf(String.valueOf(iterator.next())));
}
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
connection.commit();
connection.setAutoCommit(true);
connection.close();
}
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println(ex.getSQLState());
}
return materialList;
}
java.util.NoSuchElementException -> The folowing error occured, when counter equals 2 and an attribute cannot be assigned to preparedStatement.setString();
java.sql.Timestampwas supplanted years ago by thejava.time.Instantclass as of JSR 310 and JDBC 4.2.