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. This example will show you how to do it, and how you can utilize its benefits.

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

There are four type database JDBC connection information saved in this file, they are for Oracle, MySql, PostgreSql and Microsoft Sql Server. When you want to switch between above four database, 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.

# 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

Retrieve Database JDBC Connection Data In Java Code

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

public class LoadJDBCSettingsFromPropertiesFileExample {

	public static void main(String[] args) {
			// 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. 
			// 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. */
				// 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)



Database Name : PostgreSQL
Database Version : 9.6.4

Download “Load-JDBC-Configuration-From-Properties-File-Example.zip” Load-JDBC-Configuration-From-Properties-File-Example.zip – Downloaded 253 times – 1 KB

READ :   JDBC CallableStatement Stored Procedure Output Parameters Example

Leave a Reply

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.