ChamingaD
ChamingaD

Reputation: 2928

How to use .properties file to store database connection properties?

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

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

Note, that I haven't assigned 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;

    }        
}

Upvotes: 3

Views: 2004

Answers (4)

Roman C
Roman C

Reputation: 1

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.

Upvotes: 2

user1675825
user1675825

Reputation:

try this

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

prop.load(in);

Upvotes: 0

M Sach
M Sach

Reputation: 34424

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();

Upvotes: 2

Nazgul
Nazgul

Reputation: 1902

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).

Upvotes: 1

Related Questions