Results 1 to 2 of 2
  1. #1
    johnharris85 is offline Member
    Join Date
    Jul 2011
    Location
    Norwich, UK
    Posts
    1
    Rep Power
    0

    Default Properties file not found

    I'm following an online tutorial (MySQL Java tutorial) and I've just created a properties file to manage my mysql credentials. When I try to run the code below I can't a FileNotFoundException on "database.properties". It's located in my src folder so I don't know where the problem is? I'm using Eclipse IDE if that's relevant.

    Thanks.

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    
    public class MySqlTest {
    
        public static void main(String[] args) {
            
            Connection con = null;
    
            try {
    
                con = getConnection();
                Statement st = con.createStatement();
                ResultSet result = st.executeQuery("SELECT * FROM Authors");
    
                while (result.next()) {
                    System.out.print(result.getInt(1));
                    System.out.print(": ");
                    System.out.println(result.getString(2));
                }
    
                con.close();
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    
        public static Connection getConnection() throws FileNotFoundException,
                IOException, SQLException {
    
            Properties props = new Properties();
            FileInputStream in = new FileInputStream("database.properties");
            props.load(in);
            in.close();
    
            String url = props.getProperty("db.url");
            String user = props.getProperty("db.user");
            String passwd = props.getProperty("db.passwd");
    
            return DriverManager.getConnection(url, user, passwd);
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    To find where the current directory is create a File("dummy.txt") object and print out its absolute path.
    Compare the path that prints out with where the file you want to read is located.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 07:50 PM
  2. ResourceBundleViewResolver properties file
    By frenk_castle in forum Web Frameworks
    Replies: 1
    Last Post: 04-07-2011, 07:39 PM
  3. Unable to access velocity.properties file from jar file
    By mjwoodford in forum New To Java
    Replies: 0
    Last Post: 10-09-2009, 01:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •