Properties Files in JDBC
Properties Files in JDBC
A property file is one type of the file which organizing the data in the form of (key, value) pair. A property file is a text file which is to be created in any editors like-notepad, Editpuls and etc. Property file should be saved on same file name with on extension .prop or .rbf.
Properties Files Important points
- A property file is a text file which is to be created in any editors like-notepad, Editpuls and etc.
- Property file should be saved on same file name with on extension .prop or .rbf.
- Properties class object organizes the data in the form of (key, value) pair and it displays in the same order in whichever order it is added.
- Property file always resides in secondary memory.
java.util.properties
It is one of the pre-defined sub-class of Hashtable class, so that all the methods of Hashtable are inherited into properties class. Creating Properties is nothing but creating an object of properties class.
Syntax
Properties p=new Properties(); // Here p resides in heap memory.
Advantages of Properties class over Hashtable class
- Properties class object is able to read the data from properties/resource bundle file.
- Properties class always makes us to develop flexible application.
Constructor and Method of Properties class
| Constructor | perporties() |
| methods | public void load(FileInputStream); public String get Property(String); |
Constructor of properties class is used for creating an object of properties class.
Syntax
Properties p=new Properties();
public void load(FileInputStream); is used for loading the content of property file into properties class object by opening the properties file in read mode with the help of FileInputStream class.
Example
FileInputStream fis=new FileInputStream("student properties");
p.load(fis);
public String get Property(String); is used for obtaining the value of value by passing the key.
Example
String sno=p.getProperty("stno");
String sname=p.getProperty("sname");
String marks=p.getProperty("marks");
Example of properties file
import java.util.*;
import java.io.*;
class Prop
{
public static void main(String []args)
{
try
{
Scanner s=new Scanner(System.in);
System.out.pritnln("Enter the property file name");
String pfile=s.nextLine();
FileInputStream fis=new FileInputStream();
Properties p=new Properties();
p.load(fis);
String sno=p.getProperty();
String sname=p.getProperty();
String marks=p.getProperty();
System.out.println("student number="+sno);
System.out.println("student name="+sname);
System.out.println("student marks="+marks);
fis.close();
}
catch(FileNotFoundException fe)
{
System.out.println("properties file does not exists");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
