The data exists in a csv file. I am using a CSVReader to read each row and then store the corresponding values in the database. so while reading the strings I split the date string using "-"(date format is like 01-02-1997)and store in an array and then swap the three elements of the array and store it back in the db. Is there any better way.
Sample Row data in csv file:
10001 MICHELLE VILLEGAS3 Savings SRINATH MICHELLE.VILLEGAS3@NOBODY.COM Y 10-7-18 1050 WEST FIFTH STREET AZUSA IND 917023308
it is comma separated only.
private static void readCsv(String tableName) throws FileNotFoundException, IOException, SQLException {
CSVReader csvreader = null;
try{
Reader reader = Files.newBufferedReader(Paths.get(filePath));
csvreader = new CSVReaderBuilder(reader).withSkipLines(1).build();
sql = "insert into ? values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
ManageDBResource.createConnectionToDB();
pstmt = ManageDBResource.conn.prepareStatement(sql);
pstmt.setString(1, tableName);
String[] rowData = null;
while((rowData = csvreader.readNext()) != null) {
int i = 2;
for (String data : rowData) {
if(i == 8 && data != null && rowData[12] != "IND") {
String[] date = data.split("-");
String temp = date[0];
date[0] = date[1];
date[1] = temp;
}
pstmt.setString(i++,data);
//System.out.print(data + " ");
}
//System.out.println();
}
int result = pstmt.executeUpdate();
if(result == 1) {
System.out.println("Data loaded Successfully.");
}
}
finally {
pstmt.close();
csvreader.close();
}
How exactly do I use the STR_TO_DATE() function in the java program. sorry I am new to programming thanks for helping.