0

I have created a JDBC in Netbeans which loads data into the tables from a text file. When I run the program on a different computer, the file path (I think thats what it is called) will be different.

The text file is on my desktop. This means if I have to run the program from a different computer I would get an error because the file was obviously not found in the same place as on the original computer.

I created a string to load the data into the database, shown below:

String loadData = "load data local infile '/Users/Ricky/Desktop/table_data.txt'" +
                      "into table T1;";   

Could I create a folder in the netbeans project to store the text file? So it could be accessed from any computer with the same path?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Ricky
  • 125
  • 2
  • 9
  • A possible solution is to use [system properties](https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html) (`user.home` for example) or a custom property –  Nov 24 '15 at 21:00
  • Where do you run your program? On your desktop? Or is this a web app or something like that? Edit: also, is it always the same text file, or can the text file be different? – markspace Nov 24 '15 at 21:00
  • Take the location instead as a commandline parameter or other such mechanism. There's no sense in hardcoding the location. – Kayaman Nov 24 '15 at 21:00
  • NETNEANS (also known as NetBeans) can create many kinds of apps, including both desktop and web apps. Please answer the question explicitly. – markspace Nov 24 '15 at 21:02
  • 1
    There are several ways to solve this. Which solution to choose is up to you. Some of them: 1) if the file won't change and it depends merely on the project, then locate the file as a resource and let it be packaged as part of your jar 2) if the file can/should/must be changed by final users, then let it outside of the jar and let the client set a variable to locate the file 3) if the file can/should change its content, place it in a default place relative to the location of the jar as default, and provide to the clients the ability to set a new file and the path where this file can be consumed – Luiggi Mendoza Nov 24 '15 at 21:03
  • As Luiggi's long comment implies, there are *many* possible solutions here, and they all depend on your intended usage. There is no single "best" answer. Please give us more details. I like the command line parameter suggestion, but a simple GUI interface might also be useful. – markspace Nov 24 '15 at 21:07

3 Answers3

2

A solution is to use relative paths, as explained here

What you have to do is instead of using an absolute location (i.e. 'C:\FolderName\filename.txt'), use a relative location (i.e. 'table_data.txt') instead.

In your case, this translates to using

String loadData = "load data local infile 'table_data.txt'" +
                      "into table T1;";  

What this does is load the file from the same folder as where you ran your application from.


Another solution is to use a JFileChooser, which allows the user to find the file from whatever directory it's saved on.

Community
  • 1
  • 1
Bimde
  • 722
  • 8
  • 20
1

If you run the program from the same directory as the data file, then you could use:

String loadData = "load data local infile 'table_data.txt'" +
                  "into table T1;";

By specifying no path, the program will look in the current working directory (the directory you started the program from).

Jason
  • 11,744
  • 3
  • 42
  • 46
0

You can have an optional command line argument:

public class Main {

    private static final String LOAD_COMMAND = "load data local infile '%s' into table T1;";
    private static final String DEFAULT_FILE = System.getProperty("user.home") + "/table_data.txt";

    public static void main(String[] args) {
        String file = (args.length > 0) ? args[0] : DEFAULT_FILE;
        String command = String.format(LOAD_COMMAND, file);
        System.out.println(command);
    }
}

If you saved the above in Main.java, you could easily test it.

javac Main.java && java -cp . Main

would print:

load data local infile '/Users/<your-username>/table_data.txt' into table T1;

And

javac Main.java && java -cp . Main /my/file

would print:

# load data local infile '/my/file' into table T1;
jeremija
  • 2,338
  • 1
  • 20
  • 28