How to Read Values From Properties File in Java Example

The property file is a file we utilise in the Java Programming language to keep the configuration parameters. These files nosotros called Resource Bundle considering information technology is a packet of resources that we are going to use in the application.

What are the configuration parameters?

Configuration parameters are the parameters that y'all utilize at the time of configuring your database, or whatever validation is there in the project you can add together them as configuration parameters.

Case: Y'all must accept used DBConnect class for Database connection in your Coffee-based web application. Nosotros go on all the database URLs, database names, database drivers inside this class. Instead of writing this configuration parameter inside this form, we tin can create a organisation.properties file within your resource folder of the coffee project. The extension we employ for the properties file is .properties.

Let usa come across the properties files:

  • Everything in the backdrop file is nowadays in key-value pairs.
  • And then, in order to access any value from this file, we can use the central

Why we need a Properties file?

You can work without a backdrop file, simply the reward hither is whenever you alter this file you lot don't worry well-nigh the compilation.

 Suppose y'all 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 take to modify information technology in every module instead of what we can do, we tin create a properties file in that we can have the parameters for the path of the epitome inside this. And then, now we don't demand to modify every module. So, this is the reward of using a backdrop file.

Here, we will create Our Normal JDBCMySqLConnection Form.java

Java

package com.abc;

import coffee.sql.*;

public class DBConnect {

public static Connection getConn()

{

Connectedness con = zilch ;

String loadDriver = "com.mysql.cj.jdbc.Commuter" ;

String dbUSERNAME = "root" ;

String dbPASSWORD = "root" ;

try {

Class.forName(loadDriver);

con = DriverManager.getConnection(

dbURL, dbUSERNAME, dbPASSWORD);

Statement st = con.createStatement();

ResultSet rs

= st.executeQuery( "SELECT * FROM Customer" );

while (rs.next()) {

Organisation.out.println(

"ID -" + rs.getInt( 1 ) + " || "

+ "First-Name -" + rs.getString( 2 )

+ " || "

+ "LastName -" + rs.getString( 4 ));

}

}

take hold of (ClassNotFoundException e) {

e.printStackTrace();

}

take hold of (SQLException e) {

eastward.printStackTrace();

}

return con;

}

public static void principal(Cord[] args)

{

DBConnect.getConn();

}

}

Output:

This is the DBConnect.coffee file in the Java projection. In this, nosotros have used all the parameters within this class, So, whenever your customer wants to change the parameters he/she has to get to this file and accept to alter the things appropriately. Simply instead, what we tin can exercise here we can go on all these configuration parameters in the system.properties file.

Allow the states create a organization.properties file in coffee.

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

Coffee

url=jdbc:mysql:

driver=com.mysql.cj.jdbc.Driver

userName=root

password=root

At present, every bit you can encounter we accept a holding file where we kept all these parameters similar URL, driver, username, and password of the database. We demand to access this property file inside your Java course.

Let us see how nosotros tin access this file

We have to employ the ResourceBundle class and Take to access using a key.

  • First, nosotros will create the object of the resources bundle and will laissez passer the properties file proper name to information technology.
  • Next, nosotros take to use the getString(key) method to call the value.

Java

package com;

import coffee.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.ResourceBundle;

public course DBConnect {

public static Connection getConn()

{

ResourceBundle rd

= ResourceBundle.getBundle( "arrangement" );

Connection con = naught ;

String loadDriver = rd.getString( "commuter" );

String dbURL = rd.getString( "url" );

String dbUSERNAME = rd.getString( "userName" );

String dbPASSWORD = rd.getString( "countersign" );

endeavor {

Class.forName(loadDriver);

con = DriverManager.getConnection(

dbURL, dbUSERNAME, dbPASSWORD);

Statement st = con.createStatement();

ResultSet rs

= st.executeQuery( "SELECT * FROM Client" );

while (rs.adjacent()) {

Organisation.out.println(

"ID -" + rs.getInt( one ) + " || "

+ "Get-go-Name -" + rs.getString( 2 )

+ " || "

+ "LastName -" + rs.getString( 4 ));

}

}

grab (ClassNotFoundException east) {

e.printStackTrace();

}

take hold of (SQLException e) {

eastward.printStackTrace();

}

return con;

}

public static void main(String[] args)

{

DBConnect.getConn();

}

}

Output:


hallertrong1980.blogspot.com

Source: https://www.geeksforgeeks.org/reading-and-writing-properties-file-in-java/

0 Response to "How to Read Values From Properties File in Java Example"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel