How To Load JDBC Configuration From Properties File Example

If you need to switch between different databases using JDBC, you can store different database JDBC connection-related information in a properties file. And then you can load the JDBC properties data in java source code to use them. This example will show you how to do it, and how you can utilize its benefits.

1. Create JDBC Information Properties File.

First, you need to create a properties file like below. In this example, the file name is JDBCSettings.properties, and it is saved at C:\WorkSpace. The properties file’s content is key-value pairs. And the # character at each line’s beginning is the line comments character.

There are four types of database JDBC connection information saved in this file, they are for Oracle, MySql, PostgreSQL, and Microsoft SQL Server. When you want to switch between the above four databases, you can just uncomment the data for your database.

# Below are oracle jdbc connection configurations.
# db.driver.class=oracle.jdbc.driver.OracleDriver
# db.conn.url=jdbc:oracle:thin:@localhost:1521:DataTiger
# db.username=system
# db.password=manager
 
# Below are Mysql jdbc connection configurations.
# db.driver.class=com.mysql.jdbc.Driver
# db.conn.url=jdbc:mysql://localhost:3306/test
# db.username=root
# db.password=

# Below are Postgre sql jdbc connection configurations.
db.driver.class=org.postgresql.Driver
db.conn.url=jdbc:postgresql://localhost:5432/student
db.username=postgres
db.password=postgres

# Below are Microsoft sql server jdbc connection configurations.
# db.driver.class=com.microsoft.sqlserver.jdbc.SQLServerDriver
# db.conn.url=jdbc:sqlserver://localhost:1433;databaseName=TestDB
# db.username=sa
# db.password=008632

2. Retrieve Database JDBC Connection Data In Java Code.

Now you can get the above connection data in your java code by using the class java.util.Properties as below. Please see the code comments for detailed explanation.

public class LoadJDBCSettingsFromPropertiesFileExample {

    public static void main(String[] args) {
		
	try
	{
		// Create Properties object.
		Properties props = new Properties();
			
		String dbSettingsPropertyFile = "C:\\WorkSpace\\JDBCSettings.properties";
		// Properties will use a FileReader object as input.
		FileReader fReader = new FileReader(dbSettingsPropertyFile);
			
		// Load jdbc related properties in above file. 
		props.load(fReader);
			
		// Get each property value.
		String dbDriverClass = props.getProperty("db.driver.class");
			
		String dbConnUrl = props.getProperty("db.conn.url");
			
		String dbUserName = props.getProperty("db.username");
			
		String dbPassword = props.getProperty("db.password");
			
		if(!"".equals(dbDriverClass) && !"".equals(dbConnUrl))
		{
			/* Register jdbc driver class. */
			Class.forName(dbDriverClass);
				
			// Get database connection object.
			Connection dbConn = DriverManager.getConnection(dbConnUrl, dbUserName, dbPassword);
			
			// Get dtabase meta data.
			DatabaseMetaData dbMetaData = dbConn.getMetaData();
				
			// Get database name.
			String dbName = dbMetaData.getDatabaseProductName();
				
			// Get database version.
			String dbVersion = dbMetaData.getDatabaseProductVersion();
				
			System.out.println("Database Name : " + dbName);
				
			System.out.println("Database Version : " + dbVersion);
		}
		
	}catch(Exception ex)
	{
		ex.printStackTrace();
	}
   }

}

When you run the above java source code, you can get the below output.

Database Name : PostgreSQL
Database Version : 9.6.4

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.