Recent Posts

Thursday, 15 November 2018

About Properties Class

     In Java Program if anything which changes frequently(like jdbc url, username, password etc) is not recommended to hard code in our program. The problem in this approach is if there is any change in java program,to reflect that change we have to recompile,rebuild and redeploy total application and even some times server restart also required,which creates a big business impact to the client.

    To overcome this problem, we should go for Properties file. The variable things we have to configure in Properties file and we have to read these properties from java program.

      The main advantage of this approach is if there is any change in Properties file and to reflect that change just redeployment is enough, which won't create any business impact to the client.
url =  jdbc:oracle:thin:@localhost:1521:XE
user = scott
password = tiger
import java.sql.*;
 * @author ashok.mariyala
public class JdbcPropertiesTest { 
   public static void main(String[] args) throws Exception {
      Properties prop = new Properties(); 
      FileInputStream fis = new FileInputStream(""); 
      prop.load(fis);// to load all properties from properties file into java Properties object
      String url = prop.getProperty("url"); 
      String user = prop.getProperty("user"); 
      String password = p.getProperty("password");
      String sqlQuery = "select * from emp"; 
      Connection con = DriverManager.getConnection(url,user,pwd); 
      Statement st = con.createStatement();
      boolean flag = false;
      ResultSet rs = st.executeQuery(sqlQuery);
      while( {
      if(flag==false) {
         System.out.println("No Records found");
     If we change the properties in properties file for mysql database then the program will fetch data from MySQL database.
url =  jdbc:mysql://localhost:3306/ashokdb
user = ashok
password = ashok

How many getConnection() methods are available in DriverManager class. 
1. Connection con=DM.getConnection(url,user,pwd);
2. Connection con=DM.getConnection(url,Properties);
3. Connection con=DM.getConnection(url);

Properties p = new Properties();
FileInputStream fis = new FileInputStream("");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", p);

// MySQL
Connection con = DM.getConnection("jdbc:mysql://localhost:3306/ashok?user=root&password=root");

// Oracle
Connection con=DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@localhost:1521:XE");

Next Tutorial : Transaction Management in JDBC

Previous Tutorial : Connection Pooling

No comments:

Post a Comment