JDBC Connection using properties file in Java


On this page, we will learn how to create the JDBC connection through the property file. To access the parameters details from the properties file we can use the ResourceBunndle class of Java. Sometimes hardcoded parameters in connection object is not considered well in terms of programming. The ResourceBundle class loads parameters information’s from the properties file that contains the details.

Here is the properties file details that contains the connection parameter for Oracle and MySQL.

database.properties
#MySQL database credentials 
MySQL.path=jdbc:mysql://127.0.0.1:3306/websparrow
MySQL.user=root
MySQL.pass=

#Oracle database credentials 
Oracle.path=jdbc:oracle:thin:@localhost:1521:xe
Oracle.user=root
Oracle.pass=root

Note: Make sure your properties file must be parallel to the package.

In this example I have created the two separate class. One is for Oracle database connectivity and second is for MySQL database connectivity.

To work with OracleDataSource or MysqlDataSource these JAR files must be in your project lib.

  1. ojdbc14 – for Oracle database
  2. mysql-connector-java-5.1.x – for MySQL database

Oracle Database Example

This example will help you to connect with Oracle database.

OracleDatabaseConnectivity.java
package org.websparrow;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.sql.DataSource;

import oracle.jdbc.pool.OracleDataSource;

public class OracleDatabaseConnectivity {
	public static void main(String[] args) throws SQLException {

		Connection connection = null;

		try {
			ResourceBundle bundle = ResourceBundle.getBundle("database");

			String path = bundle.getString("Oracle.path");
			String user = bundle.getString("Oracle.user");
			String pass = bundle.getString("Oracle.pass");

			OracleDataSource oracleDataSource = new OracleDataSource();
			oracleDataSource.setURL(path);
			oracleDataSource.setUser(user);
			oracleDataSource.setPassword(pass);

			DataSource dataSource = oracleDataSource;
			connection = dataSource.getConnection();

			if (connection != null) {
				System.out.println("Connection successfull.");
			}

		} catch (Exception e) {
			System.out.println("Connection failure. See the console for more details...");
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.close();
			}
		}
	}
}

MySQL Database Example

This example will help you to connect with MySQL database.

MySQLDatabaseConnectivity.java
package org.websparrow;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.sql.DataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class MySQLDatabaseConnectivity {

	public static void main(String[] args) throws SQLException {

		Connection connection = null;

		try {
			ResourceBundle bundle = ResourceBundle.getBundle("database");

			String path = bundle.getString("MySQL.path");
			String user = bundle.getString("MySQL.user");
			String pass = bundle.getString("MySQL.pass");

			MysqlDataSource mysqlDataSource = new MysqlDataSource();
			mysqlDataSource.setURL(path);
			mysqlDataSource.setUser(user);
			mysqlDataSource.setPassword(pass);

			DataSource dataSource = mysqlDataSource;
			connection = dataSource.getConnection();

			if (connection != null) {
				System.out.println("Connection successfull.");
			}

		} catch (Exception e) {
			System.out.println("Connection failure. See the console for more details...");
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.close();
			}
		}
	}
}
Output:

In both case Oracle/MySQL, if a connection has been successfully established it will print the Connection successful message.

References

  1. ResourceBundle

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.