I currently have a simple program that inserts data into a mysql database table like so:
System.out.println("New path created: " + newPath);
String newPathfinal = newPath.toString();
try (Connection connection = DBConnection.getConnection())
{
String file = "C:/Users/User/Desktop/Test/" + newPathfinal;
String loadQuery = "LOAD DATA LOCAL INFILE '" + file + "' INTO TABLE txn_tbl FIELDS TERMINATED BY ','"
+ " LINES TERMINATED BY '\n' " + "IGNORE 1 LINES(txn_amount, card_number, terminal_id)";
System.out.println(loadQuery);
Statement stmt = connection.createStatement();
stmt.execute(loadQuery);
System.out.println("Data import success");
}
catch (Exception e)
{
e.printStackTrace();
}
However the incoming files vary in names and their content(columns are different) so i want to be filter the files by name into different LOAD DATA LOCAL INFILE commands so that the data can be inserted correctly.
I was thinking along the lines of using a simple if-else statement since i decreed the filenames as String but i realised if-else statements take the FULL filename as a condition, and this would not work in the case i want to filter by partial filename. Like so: if(newPathfinal == “apple" {}
I want all files containing the word "apple" to be imported into a certain table.
For example, let's say i want to import files with name starting from "java" like java_1 into mySQL table java1. And files named "apple" like apple_1 into apple1. java_2 and apple_2 proceed to be imported and i need to correspondingly filter these into the right LOAD DATA LOCAL INFILE statements to insert their data into the right tables.
Is there any way i can filter by partial filename? I read up on jfilechooser and fileFilter but these seem to focus on the file extensions which is not my area of focus since the files are all in the same .csv format.
Many thanks once again!