Open In App

Reading and Writing Properties File in Java

The property file is a file we use in the Java Programming language to keep the configuration parameters. These files we called Resource Bundle because it is a bundle of resources that we are going to use in the application.

What are the configuration parameters?



Configuration parameters are the parameters that you use at the time of configuring your database, or any validation is there in the project you can add them as configuration parameters.

Example: You must have used DBConnect class for Database connection in your Java-based web application. We keep all the database URLs, database names, database drivers inside this class. Instead of writing this configuration parameter inside this class, we can create a system.properties file inside your resource folder of the java project. The extension we use for the properties file is .properties.



Let us see the properties files:

Why we need a Properties file?

You can work without a properties file, but the advantage here is whenever you alter this file you don’t worry about the compilation.

 Suppose you are creating a Java web-based project in that you are using some image path and that image path is used in different modules of your project. So, if you want to alter something in the path you have to change it in every module instead of what we can do, we can create a properties file in that we can have the parameters for the path of the image inside this. So, now we don’t need to alter every module. So, this is the advantage of using a properties file.

Here, we will create Our Normal JDBCMySqLConnection Class.java




package com.abc;
 
import java.sql.*;
 
public class DBConnect {
 
    public static Connection getConn()
    {
        Connection con = null;
 
        // driver name for mysql
        String loadDriver = "com.mysql.cj.jdbc.Driver";
 
        // url of the
        String dbURL = "jdbc:mysql://localhost:3306/DbName";
        // database
 
        // username to connect db
        String dbUSERNAME = "root";
 
        // password to connect db
        String dbPASSWORD = "root";
 
        try {
            // load the driver
            Class.forName(loadDriver);
            con = DriverManager.getConnection(
                dbURL, dbUSERNAME, dbPASSWORD);
 
            // get the connection
            Statement st = con.createStatement();
            ResultSet rs
                = st.executeQuery("SELECT * FROM CUSTOMER");
            while (rs.next()) {
                System.out.println(
                    "ID -" + rs.getInt(1) + " || "
                    + "First-Name -" + rs.getString(2)
                    + " || "
                    + "LastName -" + rs.getString(4));
            }
        }
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return con; // return the connection obj.
    }
    public static void main(String[] args)
    {
        DBConnect.getConn();
    }
}

Output:

This is the DBConnect.java file in the Java project. In this, we have used all the parameters inside this class, So, whenever your client wants to change the parameters he/she has to go to this file and have to change the things accordingly. But instead, what we can do here we can keep all these configuration parameters in the system.properties file.

Let us create a system.properties file in java.

Just right-click on your resources folder and create a property file. 




url=jdbc:mysql://localhost:3306/DBName
driver=com.mysql.cj.jdbc.Driver //Driver class of mysql
userName=root //Db username
password=root //Db password

Now, as you can see we have a property file where we kept all these parameters like URL, driver, username, and password of the database. We need to access this property file inside your Java class. 

Let us see how we can access this file

We have to use the ResourceBundle class and Have to access using a key.




package com;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
 
public class DBConnect {
 
    public static Connection getConn()
    {
        ResourceBundle rd
            = ResourceBundle.getBundle("system");
        Connection con = null;
 
        // driver name for mysql
        String loadDriver = rd.getString("driver");
 
        // url of the database
        String dbURL = rd.getString("url");
 
        // username to connect db
        String dbUSERNAME = rd.getString("userName");
 
        // password to connect db
        String dbPASSWORD = rd.getString("password");
 
        try {
            // load the driver
            Class.forName(loadDriver);
 
            // get the connection
            con = DriverManager.getConnection(
                dbURL, dbUSERNAME, dbPASSWORD);
            Statement st = con.createStatement();
            ResultSet rs
                = st.executeQuery("SELECT * FROM CUSTOMER");
            while (rs.next()) {
                System.out.println(
                    "ID -" + rs.getInt(1) + " || "
                    + "First-Name -" + rs.getString(2)
                    + " || "
                    + "LastName -" + rs.getString(4));
            }
        }
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return con;
    }
    public static void main(String[] args)
    {
        DBConnect.getConn();
    }
}

Output:


Article Tags :