Why all my excel data do not insert into database?
Here is the sample coding that i made. Can help me. ? what is wrong with this coding.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.joget.apps.app.service.AppUtil;
import org.joget.workflow.model.service.WorkflowManager;
import java.util.Map;
import java.util.HashMap;
/* Get the newest file for a specific extension */
public File getTheNewestFile(String filePath, String ext) {
File theNewestFile = null;
File dir = new File(filePath);
FileFilter fileFilter = new WildcardFileFilter("*." + ext);
File[] files = dir.listFiles(fileFilter);
if (files.length > 0) {
/** The newest file comes first **/
Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
theNewestFile = files[0];
}
return theNewestFile;
}
public void readCSV(String csvFile){
String line = "";
String cvsSplitBy = ",";
try{
BufferedReader br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
// Check the file betul ke masuk.
String[] record = line.split(cvsSplitBy);
String locNo = record[0];
String materialType = record[3];
String materialCode = record[5];
String plant = record[8];
String statusChecker = record[38];
String statusApprover = record[39];
String statusVerifier = record[40];
String locPreparedBy = record[16];
System.out.println("\n-------- Record --------- \n [LOC No.= " + locNo
+ "\nChecker Status=" + statusChecker
+ "\nApprover Status=" + statusApprover
+ "\nVerifier Status=" + statusVerifier
+ "\nPlant = " + plant
+ "\nMaterial Type = " + materialType
+ "\nMaterial Code = " + materialCode + "]\n ---------------------------");
// bila data2 baru masuk dia akan check ini semua baru start the new process
if(statusChecker.equalsIgnoreCase("") && statusApprover.equalsIgnoreCase("") && statusVerifier.equalsIgnoreCase(""))
{
System.out.println("\n SHOULD START WITH NEW PROCESS");
//handleNewProcess(record[]); // start new process
//handleNewProcess(record);
//WorkflowManager.startProcess();
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
workflowManager.processStart("SAPLOC#latest#process2", null, null, locPreparedBy, "#form.SAP_LOC.id#", false);
}
else if(statusChecker.equalsIgnoreCase("Approved") && statusApprover.equalsIgnoreCase("Approved") && statusVerifier.equalsIgnoreCase("Approved") ){
System.out.println("You can logik lah");
}
else
{
System.out.println("do nothing");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("\n\n");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));
System.out.println("Anda Telah Berjaya\n");
File latestCSVFile = getTheNewestFile("/JogetLOC/", "csv");
System.out.println("latest file is --> " + latestCSVFile.getAbsolutePath());
readCSV(latestCSVFile.getAbsolutePath());