2

I have to build an SQL query where I have to replace two params in the String.

  • The String variable CLEAN_CSV_PATH which represents a path
  • The String variable LINES_TERMINATED_OS which represents a the escape character \r\n that are needed for windows or linux, depends.

The params are provided during Main execution with the Scanner class.

This is my SQL String :

private static String LOAD_ALL_ALIASES = //

        "LOAD DATA LOCAL INFILE '" + CLEAN_CSV_PATH + "' INTO TABLE plantbiocore.Alias " //
                + "FIELDS TERMINATED BY ',' " //
                + "OPTIONALLY ENCLOSED BY  '\"' "
                + "LINES TERMINATED BY '" + LINES_TERMINATED_OS + "'"
                + "IGNORE 1 LINES " //
                + "(locus_id, organism_id, variable, alias) "; //



 Scanner scanner = new Scanner(System.in);
 CLEAN_CSV_PATH = scanner.next() // Here the CLEAN_CSV_PATH does not appear in my LOAD_ALL_ALIASES 

3 Answers 3

1

What you need is called PreparedStatement in java. Google it, there are many examples of using this class. The other problem is that you are setting CLEAN_CSV_PATH after you used it to set LOAD_ALL_ALIASES. It can't work in Java.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to do the things in the reverse way.
First set CLEAN_CSV_PATH with the scanner. Then resolve your String with.
To allow to create queries with different CLEAN_CSV_PATH, you could extract it into a method that accepts a parameter.

public void foo(){
   Scanner scanner = new Scanner(System.in);
   String csvPath = scanner.next()
   String query = getQuery(csvPath);         
   // ...
}

private static String getQuery(String csvPath){

       return "LOAD DATA LOCAL INFILE '" + csvPath + "' INTO TABLE plantbiocore.Alias " //
                + "FIELDS TERMINATED BY ',' " //
                + "OPTIONALLY ENCLOSED BY  '\"' "
                + "LINES TERMINATED BY '" + LINES_TERMINATED_OS + "'"
                + "IGNORE 1 LINES " //
                + "(locus_id, organism_id, variable, alias) "; //
}

Note that LINES_TERMINATED_OS could be replaced by System.lineSeparator() that returns at runtime the OS dependent line separator.

Comments

0

Write it in the right sequence then it will work fine:

    String LOAD_ALL_ALIASES;
    String CLEAN_CSV_PATH;
    String LINES_TERMINATED_OS;

    Scanner scanner = new Scanner(System.in);
    CLEAN_CSV_PATH = scanner.next();
    LINES_TERMINATED_OS = scanner.next();

    LOAD_ALL_ALIASES = "LOAD DATA LOCAL INFILE '" + CLEAN_CSV_PATH + "' INTO TABLE plantbiocore.Alias " //
            + "FIELDS TERMINATED BY ',' " //
            + "OPTIONALLY ENCLOSED BY  '\"' "
            + "LINES TERMINATED BY '" + LINES_TERMINATED_OS + "'"
            + "IGNORE 1 LINES " //
            + "(locus_id, organism_id, variable, alias) ";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.