2

I'm running Mac OS X and the code works. My teammate is running Windows 7 and he gets the error (even though the directory structure and file exists on his system). Any ideas? I have spent a number of hours on this and can't figure it out. Thank you.

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; // Not used
import java.sql.ResultSet; // Not used
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; // Not used
import java.util.Scanner; // Not used

public class CreateDB {

   public CreateDB() {
   }

   public void createDB() {

      Connection conn = null;

      String url = "jdbc:mysql://localhost:3306/";
      String dbName = ""; // Not used (yet?)
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root"; // Tell user userName is "root". OR/TODO: Change to something more secure?
      String password = ""; // Tell user password is "". Or/TODO: Change to something more secure?
      String line;
      String sqlDump = null;
      String [] sqlDumpArray;

      String fileSeperator = System.getProperty("file.separator");
      System.out.println("fileSeperator is: " + fileSeperator);  // Testing
      String path = System.getProperty("user.dir");
      System.out.println("path is: " + path); // Testing
      String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
      System.out.println("pathToFile is: " + pathToFile); // Testing.
      String fileName = "create_db_and_tables.sql"; // NOTE: Rename file, if necessary. Make sure it's in right place.
      System.out.println("pathToFile + fileName is: " + pathToFile + fileName); // Testing.

      try {
         Class.forName(driver).newInstance();
         conn = DriverManager.getConnection(url, userName, password);
         System.out.println("Connected to the database.\n"); // Testing

         BufferedReader reader = new BufferedReader(new FileReader(pathToFile + fileName));
         line = reader.readLine();
         while (line != null) {
            sqlDump += line + "\n";
            line = reader.readLine();
         }

         System.out.println(sqlDump); // Testing
         sqlDumpArray = sqlDump.split(";");
         System.out.println("sqlDumpArray size is " + sqlDumpArray.length); // Testing

         for (int i = 0; i < sqlDumpArray.length; i++) {
            System.out.println("\nsqlDumpArray[" + i + "] is: " + sqlDumpArray[i]);  // Testing
         }

         for (int j = 1; j < sqlDumpArray.length -1; j++) {
            try {
               Statement statement = conn.createStatement();
               statement.addBatch(sqlDumpArray[j]);
               statement.executeBatch(); // Could refactor or us another method
            }
            catch (SQLException e) {
               System.err.println("SQLException: " + e.getMessage());

            }
            catch (Exception e) {
               System.err.println("Exception: " + e.getMessage());
            }
         }
      }
      catch (java.lang.ClassNotFoundException e) {
         System.err.println("ClassNotFoundException: " + e.getMessage());

      }
      catch (SQLException e) {
         System.err.println("SQLException: " + e.getMessage());

      }
      catch (Exception e) {
         System.err.println("Exception: " + e.getMessage());
      }
      finally {
         try {
            // conn.commit(); // Not necessary
            conn.close();
            System.out.println("Successfully disconnected from database. Yeah!");
         }
         catch (SQLException e) {
            System.err.println("SQLException in 'finally' block: " + e.getMessage());
         }
      }
   }
   public static void main(String[] args) {
     CreateDB create = new CreateDB();
     create.createDB();
   }
}

Users on Windows have received the error when when trying to execute the database creation process. This error occurs even when the path and file appear to be correct, such as what is returned by the following System.out.println statement in the file (highlighted below):

String fileSeperator = System.getProperty("file.separator");
System.out.println("fileSeperator is: " + fileSeperator);
String path = System.getProperty("user.dir");
System.out.println("path is: " + path);
String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
System.out.println("pathToFile is: " + pathToFile);
String fileName = "create_db_and_tables.sql";
System.out.println("pathToFile + fileName is: " + pathToFile + fileName);

This is an example of what that statement returns on a Windows 7 computer:

Exception: C:\Users\USER\workspace\mrbs2011a\src\files\create_db_and_tables.sql (The system cannot find the path specified)

That gets printed out even though USER has the file create_db_and_tables.sql located in the src/files folder.

4
  • What lines has the error? What does the fill file path look like on Mac OS X vs. on Windows 7? Commented Dec 8, 2011 at 13:23
  • where are you getting the exception ? stacktrace please Commented Dec 8, 2011 at 13:24
  • Hi, thanks for the comments so far. edited the question, adding the exception reported to me (see bottom of question). And I added e.printStackTrace(); to all the catch block and have asked my teammate to run it again and send me everything that prints in his console. He is out at a doctors appointment now, so might be a bit before I can reply with the details. In the meantime, if anyone is on Windows 7 and can spare a few minutes, can you try it and see what you get? Thanks. Commented Dec 8, 2011 at 13:37
  • On Mac OS X, I get these results from the applicable System.out.println statements: fileSeperator is: / path is: /Users/chris/Documents/workspace/mrbs2011a pathToFile is: /Users/chris/Documents/workspace/mrbs2011a/src/files/ pathToFile + fileName is: /Users/chris/Documents/workspace/mrbs2011a/src/files/create_db_and_tables.sql On Windows 7, this is what I have reported to me so far (can test it on your systems if you use the applicable lines of code): path is: C:\Users\USER\workspace\mrbs2011a pathToFile is: C:\Users\USER\workspace\mrbs2011a\src\files\ Commented Dec 8, 2011 at 13:40

1 Answer 1

1

It's possible that the file is locked or that the user privileges under which the Java program is running don't allow access to that file.

If the file is actually there, then the file being locked (e.g. being held open for writing by another process) is probably the reason. The documentation for FileReader says that it will throw FileNotFoundException if "the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading."

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

1 Comment

Yes, FileNotFoundException is actually used in a variety of cases besides the file not existing. This is done in some cases to prevent leaking information to an attacker about the contents of the file system.

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.