0

In the code below, I parsed all the filenames, then with it, I am trying to check the database to see if it exists in it or not. What am I doing wrong below? If so, can you let me know?

Also in execute command, I get an error saying listofFiles cannot be resolved to variable.

public class FileGetName {
public String fileName;
    public static void main(String[] args) {
        File folder = new File("C");
        File[] listOfFiles = folder.listFiles();

            for (int i = 0; i < listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                System.out.println(listOfFiles[i].getName());
              } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
              }
            }
    }

    public void doesFileExistinDB(String fileName) {
        PreparedStatement pst = null;
        Connection conn = null;
        ResultSet rs= null;
        try {
            conn = DBConnect.getInstance().dbOracleConnect();    
            String sql= "select * from PO_Parent_List where" +
            " po_number in (fileName)";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
        }
        catch (Exception e) {
            System.out.println(e);
            }
        }



    public void execute() {
        if (listOfFiles[i].isFile())
        {
            String fileName = listOfFiles[i].getName();
            System.out.println(fileName);
            doesFileExistInDb(fileName);
        }
    }
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • You are not doing anything with the `ResultSet` returned by the query. Consider returning a boolean value on the `doesFileExistinDB` method to show if the entry exists/doesn't exist in the database. – Raul Rene Jun 10 '13 at 16:39
  • `new File("C")` <-- I hope this is not meant to be Windows' C: drive? – fge Jun 10 '13 at 16:40
  • Note: This is a follow up to [this](http://stackoverflow.com/q/17027921/1639625) question, and I assume OP put the code from the answer into the wrong place... when is `execute` called anyway? – tobias_k Jun 10 '13 at 17:57

4 Answers4

2

You need to pass listOfFiles in public void execute() method like:

public void execute(File[] listOfFiles){

}

That is why you are getting compilation error as listOfFiles can not be resolved to variable.

Or you need to declare the variable listOfFiles into the class level.

Edit: (Not able to understand from where you are calling execute)

int i = 0;//Declare i to class level.
public void execute(File[] listOfFiles) {
        if (listOfFiles[i].isFile())
        {
            String fileName = listOfFiles[i].getName();
            System.out.println(fileName);
            doesFileExistInDb(fileName);
            i++;
        }
    }
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

listOfFiles is specific to the main() method. To make it visible to other portions of your class, you must declare it as a instance variable, for example:

public class FileGetName {
    public String fileName;
    public File[] listOfFiles;

Your method doesFileExistinDB() should return boolean and test if rs returns one more results in order to make sure the file name is found. See Java ResultSet how to check if there are any results for examples.

Community
  • 1
  • 1
Piovezan
  • 3,215
  • 1
  • 28
  • 45
0

2 things:

  1. Your fileName is not being passed in the query. You are basically hardcoding the String "fileName" instead of your fileName.

  2. Consider returning a boolean value to show if the file exists or not (if the resultSet has at least 1 entry, your file exists):

Thus, your method becomes:

public boolean doesFileExistinDB(String fileName) {
    PreparedStatement pst = null;
    Connection conn = null;
    ResultSet rs= null;
    try {
        conn = DBConnect.getInstance().dbOracleConnect();    
        String sql= "select * from PO_Parent_List where" +
        " po_number in (?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1, fileName); 
        rs = pst.executeQuery();
        if (rs.next) {
            //You have data!
            return true;
        }
    }
    catch (Exception e) {
        System.out.println(e);
    }
    return false;
}
Raul Rene
  • 10,014
  • 9
  • 53
  • 75
0

listofFiles is declared in your main not in your function execute(). You can pass an the listofFiles by adding an argument to your function.

public void execute(File[] listOfFiles){

}

But you will encounter others problem. In your execute(). When you're doing :

if (listOfFiles[i].isFile())

The variable 'i' is not initialized, but I think you're refering to :

for (int i = 0; i < listOfFiles.length; i++)

If you want to acces i, you will need call execute in you for. Your method execute should then look like :

public void execute(File[] listOfFiles, int i){

}

EDIT: Be sure to look at all the answers, your code need more modifications and many people have pointed some things you should change.

Marc-Andre
  • 912
  • 1
  • 15
  • 34