Problem reading properties file

0

I created a propertie file for the bank to read this file from there but the console gives me the following error:

Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
Erronull\properties\conexao.propertie (O sistema não pode encontrar o caminho especificado)
ConexaoMySQL: The url cannot be null
java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at modelo.ConexaoMySQL.conectar(ConexaoMySQL.java:18)
    at helper.BancoDadosHelper.getNomeColunas(BancoDadosHelper.java:18)
    at principal.Main.testaConexDB(Main.java:38)
    at principal.Main.menu(Main.java:30)
    at principal.Main.main(Main.java:17)
Exception in thread "main" java.lang.NullPointerException
    at helper.BancoDadosHelper.getNomeColunas(BancoDadosHelper.java:21)
    at principal.Main.testaConexDB(Main.java:38)
    at principal.Main.menu(Main.java:30)
    at principal.Main.main(Main.java:17)

MySql Connection Class:

package modelo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConexaoMySQL {

    private static Connection conexao;

    public static boolean conectar() {
        Propriedade.setPath(System.getProperty("jdbc.url")+"\properties\conexao.propertie");
        String url = Propriedade.getValor("url");
        String usr = Propriedade.getValor("user");
        String pwd = Propriedade.getValor("password");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexao = DriverManager.getConnection(url,usr,pwd);
            System.out.println("ConexaoMySQL.conectar");
            return true;
        } catch (Exception e) {
            System.out.println("ConexaoMySQL: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public static void fecharConexao() {
        if (conexao != null) {
            try {
                conexao.close();
                conexao = null;
                System.out.println("ConexaoMySQL.fecharConexao");
            } catch (SQLException e) {
                System.out.println("ConexaoMySQL: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public static Connection getConexao(){
        return conexao;
    }

}

Class

package modelo;

import java.io.FileInputStream;
import java.util.Properties;

public class Propriedade {

    private static String path;

    public static void setPath(String caminhoDoArquivo) {
        path = caminhoDoArquivo;
    }

    public static String getValor(String key) {
        String returning = null;
        FileInputStream fis = null;
        Properties prop = new Properties();
        try {
            fis = new FileInputStream(path);
            prop.load(fis);
            returning = prop.getProperty(key);
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            System.out.println("Erro" + e.getMessage());
        }
        return returning;
    }

}

File connection.propertie:

url = jdbc:mysql://localhost:3306/sistema;
user = root;
password = ;
    
asked by anonymous 08.06.2015 / 14:28

1 answer

1

Both your property file and the code that retrieves it are wrong, being the root of the error due to the fact that the file can not be found.

The displayed error is:

  

The system can not find the path specified

See that you are retrieving ( System.getProperty("jdbc.url") ) a system property that is currently non-existent, to form path and is returning null , the final path generated being equal to null\properties\conexao.propertie

For this, consider recovering the properties file as a resource , if it is in your application's classpath, something like this:

InputStream fis = Propriedade.class.getResourceAsStream("/properties/conexao.propertie")

This makes unnecessary setPath of class Propriedade . Another note is that you can load the properties file only once, in a static block yourself.

Then, in the end, the class Propriedade would look like this:

public final class Propriedade {

    private Propriedade() {}

    private static final Properties prop = new Properties();

    static {
        try (final InputStream fis = Propriedade.class.getResourceAsStream("/properties/conexao.propertie")) {
            prop.load(fis);
        } catch (final IOException e) {
            throw new ExceptionInInitializerError(e.getMessage());
        }
    }

    public static String getValor(final String key) {
        final String returning = prop.getProperty(key);
        return returning != null ? returning : key;
    }

}

Finally, considering the contents of your properties file:

url = jdbc:mysql://localhost:3306/sistema;
user = root;
password = ;

This will always return ; which, in the case of user and password, will probably generate error.

If possible, prefer this content:

url = jdbc:mysql://localhost:3306/sistema
user = root
password = 

To use is the same way you were doing before, Propriedade.getValor("propriedade") , but in conectar of ConexaoMySQL you should remove the first line of the method.

An example recovering is this:

System.out.println("Propriedade 'user' (valor=root;): " + Propriedade.getValor("user"));
System.out.println("Propriedade 'key' (inexistente): " + Propriedade.getValor("key"));

That generates this result:

Propriedade 'user' (valor=root;): root;
Propriedade 'key' (inexistente): key
    
08.06.2015 / 14:54