简体   繁体   中英

How to use Java Property File?

I need to use .properties file in Java to store database information.

Here is my database connector class. It's giving NullPointerException . What is the issue with my code ?

Note, that I haven't' assign those property file values. DB connection values are still hard coded.

import java.io.IOException;
import java.io.InputStream;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


public final class Database {

    public Connection connection;
    private Statement statement;
    private Properties property;
    public static Database database;

    private Database() {

        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "edus";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        try {
            InputStream is = Database.class.getClassLoader().getResourceAsStream(
                "config.properties");
            property.load(is);
            System.out.println(property.getProperty("db_user"));
            System.out.println(property.getProperty("db_password"));
            System.out.println(property.getProperty("db_name"));

            Class.forName(driver).newInstance();
            this.connection = (Connection) DriverManager.getConnection(url + dbName,
                    userName, password);
        }catch (IOException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException e) {
            System.out.println("JDBC driver is missing");
        } catch (InstantiationException | IllegalAccessException | SQLException e) {
            e.printStackTrace();
        } 
    }

    public static synchronized Database getDatabaseConnection() {
        if (database == null) {
            database = new Database();
        }
        return database;

    }


}

config.properties is not lying under classpath. It should be under classes folder.

you can also try

Database.class.getClassLoader().getResourceAsStream(
                "com/lk/apiit/eduservice/config.properties");

As Roman C pointed out you also need to initialize Properties Object first

Properties property = new Properties();

You forgot to initialize

Properties property = new Properties();

This is an issue of NullPointerException in your code, because you referenced not initialized variable.

If you open a stream you should close it after it's not used. Do it by adding finally block.

The code where you getting a connection to the database you can move to the corresponding method. If the connection is closed you will not reinitialize the database again just reopen a connection or get a new one.

Dont keep config properties file in a package. Keep it directly inside the source folder, so that the config properties file comes directly in the build/classes folder after the build is done.

The issue is that your config properties in in the folder com/ik/apiit/eduservice folder but your code is expecting it to be directly in the classes folder (the root folder of classpath).

try this

FileInputStream in = new FileInputStream(System.getProperty("WEB-INF/dbConnection.properties"));

prop.load(in);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM