JDBC Tutorial

Read the data from Resource Bundle File

Read the data from Resource Bundle File:

  1. In order to read the data from resource bundle file, open the resource bundle file in read mode with the help of FileInputStream class.
  2. For example:

    FileInputStream fis=new FileInputStream ("db.prop");
    
  3. Since files does not support to read the data separately in the form of (key, value). Hence, it is recommended to get the data of the file we must create an object of a predefined class called java.util.Properties
  4. For example:

    Properties p=new Properties ();
    
  5. In order to link 'fis' and 'p' objects we must use the following method:
  6. Util properties

    For example:

    p.load (fis);
    
  7. Obtain the property value by passing property name by using the following method:
  8. public Object get (String);
    public Object getProperty (String);
    

    Here, String represents property name or key name.

    For example:

    String dname= (String) p.get ("Dname"); 
    String url= (String) p.get ("URL");
    String username= (String) p.get ("Uname"); 
    String password= (String) p.get ("Pwd"); 
    String table= (String) p.get ("Tablename");
    

    Here, Dname, URL, Uname, Pwd and Tablename are the property names present in resource bundle file. dname, url, username, password and table are the property values present in resource bundle file.

Write a java program which illustrates the concept of resource bundle file or how to develop a flexible jdbc application along with its metadata?

Answer:

rbfdb.prop:

Dname=oracle.jdbc.driver.OracleDriver URL=jdbc:oracle:thin:@127.0.0.1:1521:oradsn 
Uname=scott 
Pwd=tiger 
Tablename=student