2

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!

Mack
  • 117
  • 2
  • 9
  • So you just want to change your `if(newPathfinal == “` line to filter by a partial name, pretty much? – D M Sep 18 '17 at 03:02
  • Yup! My confusion is i'm pretty sure an if-else loop only works for full names ie. has to be apple.csv but not apple_1.csv. Sorry i forgot to remove the incomplete IF statement – Mack Sep 18 '17 at 03:04
  • Feel free to edit that line. – D M Sep 18 '17 at 03:05
  • I've found a link that may help, which is substrings: https://stackoverflow.com/questions/15402101/substrings-and-if-statements-in-java I'll read up on this and report back here if it works! – Mack Sep 18 '17 at 03:08
  • Substrings would work, as would my answer. – D M Sep 18 '17 at 03:08
  • If you need some really advanced pattern matching, you could also use regular expressions, although that may be overkill for your needs. – D M Sep 18 '17 at 03:11

2 Answers2

2

It sounds like you want to use the startsWith method.

if (newPathFinal.startsWith("java"))
{
    // do java stuff
}
else if (newPathFinal.startsWith("apple"))
{
    // do apple stuff
}
D M
  • 1,410
  • 1
  • 8
  • 12
2

if statements only "take the FULL filename as a condition" if you program them that way. Using methods available from the File and String classes you can do much more.

First, File.getName() will get you the name part of file path, separate from the directory:

File file = new File("C:/Users/User/Desktop/Test/java_12345.txt");
System.out.println(file.getName()); // prints "java12345.txt"

Now you can go to work on the file name with methods from String. For your examples, startsWith() would be ideal:

if (file.getName().startsWith("apple")) {
    // handle "apple" files
}
else if (file.getName().startsWith("java")) {
    // handle "java" files
}

There's also endsWith in case you'd like to filter out specific extensions as well:

if (file.getName().startsWith("apple") && file.getName().endsWith(".txt") {
    // handle "apple*.txt" files 
}
else if (file.getName().startsWith("java")) {
    // handle "java" files
}

And that's just the easy stuff. There are all sorts of ways you can examine and test Strings that might come in handy either now or in the future. You can use substring() to peek inside the name, or even matches() for really tricky filtering.

Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21