In previous post JDBC MySQL database Connection example we learnt how to connect java program with database using various approaches.
In this post we will learn how to segregate database information like database URL,username,password etc. from source code and keeping in .properties file.
Property file(database.properties) which holds database information like database URL,username and password.
1 2 3 4 |
driver.class.name=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/jdbcdb db.username = root db.password=root |
DBUtil.java class which is responsible to connect with MySQL database.
Here You would have observed that how database.properties file is loaded using load method of java.util.Properties class.
After that we get every property value from properties file by using getProperty(String key) method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.infotech.util; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DBUtil { private static final String DB_DRIVER_CLASS="driver.class.name"; private static final String DB_USERNAME="db.username"; private static final String DB_PASSWORD="db.password"; private static final String DB_URL ="db.url"; private static Connection connection = null; private static Properties properties = null; static{ try { properties = new Properties(); properties.load(new FileInputStream("src/database.properties")); Class.forName(properties.getProperty(DB_DRIVER_CLASS)); connection = DriverManager.getConnection(properties.getProperty(DB_URL),properties.getProperty(DB_USERNAME) , properties.getProperty(DB_PASSWORD) ); } catch (ClassNotFoundException | SQLException | IOException e) { e.printStackTrace(); } } public static Connection getConnection(){ return connection; } } |
ClientTest.java client class which makes use of DBUtil to take connection from database(MySQL).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.infotech.util; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DBUtil { private static final String DB_DRIVER_CLASS="driver.class.name"; private static final String DB_USERNAME="db.username"; private static final String DB_PASSWORD="db.password"; private static final String DB_URL ="db.url"; private static Connection connection = null; private static Properties properties = null; static{ try { properties = new Properties(); properties.load(new FileInputStream("src/database.properties")); Class.forName(properties.getProperty(DB_DRIVER_CLASS)); connection = DriverManager.getConnection(properties.getProperty(DB_URL),properties.getProperty(DB_USERNAME) , properties.getProperty(DB_PASSWORD) ); } catch (ClassNotFoundException | SQLException | IOException e) { e.printStackTrace(); } } public static Connection getConnection(){ return connection; } } |
When you run ClientTest .java in your favorite IDE then you will get below out.
1 |
JDBC:connection is taken.. |
This shows we successfully connected our java program with MYSQL database.
That’s all about this topic JDBC database connection using property resource(property file)
You May Also Like:
What is Java Database Connectivity (JDBC)?
Types of JDBC drivers in java?
JDBC MySQL database Connection example
CRUD(Create,Read,Update and Delete) Operation using JDBC Statement
Reading data from database using JDBC Statement
CRUD(Create,Read,Update and Delete) Operation using JDBC PreparedStatement
If you have any feedback or suggestion please feel free to drop in blow comment box.