Error reading property file in java

1

I do not know is correct and it works, but I made a division in the project. a project would access DAO and have the connection and an properties file with the connection information. Another has a main class in which I have not created frames yet, just a main class for testing. In the libraries I added the projects as reference and I did the test but it is giving an error where it can not read the properties file, could you tell me what it can be?

Error Description:

jul 27, 2016 9:30:41 AM Conection.GenericConnection getDbProperties
GRAVE: null
java.io.FileNotFoundException: src\properties\conf.properties (O sistema não pode encontrar o caminho especificado)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at Conection.GenericConnection.getDbProperties(GenericConnection.java:25)
    at Conection.OracleConnection.getConnection(OracleConnection.java:35)
    at Views.mainTeste.main(mainTeste.java:16)

Exception in thread "main" java.lang.NullPointerException
    at Conection.OracleConnection.getConnection(OracleConnection.java:35)
    at Views.mainTeste.main(mainTeste.java:16)
D:\SICF_Project\SICFCadastros\nbproject\build-impl.xml:1063: The following error occurred while executing this line:
D:\SICF_Project\SICFCadastros\nbproject\build-impl.xml:804: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

Mother Class GenericConnection

package Conection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ulisses Gimenes
 */
public abstract class GenericConnection {

    protected Properties dbProperties;
    protected Connection conn;
    protected Statement st;

    public Properties getDbProperties() {
        if (dbProperties == null) {
            try {
                dbProperties.load(new FileInputStream("src/properties/conf.properties"));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return dbProperties;
    }

    public void setDbProperties(Properties dbProperties) {
        this.dbProperties = dbProperties;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public Statement getSt() {
        return st;
    }

    public void setSt(Statement st) {
        this.st = st;
    }

    public abstract Connection getConnection();
}

Connection class for oracle

package Conection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author UlissesGimenes
 */
public class OracleConnection extends GenericConnection {  

    private static OracleConnection connOra;

    public static OracleConnection getConnOra() {
        if(connOra == null){
            connOra = new OracleConnection();
        }
        return connOra;
    }

    public static void setConnOra(OracleConnection connOra) {
        OracleConnection.connOra = connOra;
    }    

    @Override
    public Connection getConnection() {
        if (getConn() != null) {
            return getConn();
        } else {
            try {
                String url = "jdbc:oracle:thin:@" + 
                        super.getDbProperties().getProperty("ServerOracle") + 
                        ":" + super.getDbProperties().getProperty("portOracle") + 
                        ":" + super.getDbProperties().getProperty("sidOracle");
                setConn(DriverManager.getConnection(
                        url, 
                        super.getDbProperties().getProperty("userOracle"), 
                        super.getDbProperties().getProperty("passwdOracle")));
                setSt(getConn().createStatement());
                System.out.println("conectado");
                return getConn();
            } catch (SQLException ex) {
                Logger.getLogger(OracleConnection.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
    }

}

Test main class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Conection.OracleConnection;

/**
 *
 * @author UlissesGimenes
 */
public class mainTeste {
    public static void main(String[] args) {
        OracleConnection.getConnOra().getConnection();
    }
}

Project tree image

    
asked by anonymous 27.07.2016 / 15:00

2 answers

1

Your problem is in detecting the correct path of the .properties file in the project. The question is that the way you are accessing the file, it should be in the working directory, which can be accessed like this:

System.getProperty("user.dir")

Now, if you do not want to leave the .properties file in the working directory and prefer that it go packaged in the project, then you should access it via getClass () or > getClass (). getResourceAsStream () .

    
27.07.2016 / 16:00
1

Apparently you're given the wrong path to the file. I do not know if I interpreted correctly to your project tree because I do not work with this IDE but try to change this path:

src/properties/conf.properties

By this path:

properties/conf.properties

It is always better to keep this type of configuration file in the project's root path, so it is easier to find. Just grab the main project and create a folder inside it properties and put the file in there.

    
27.07.2016 / 19:05