Rachel
Rachel

Reputation: 1159

Getting values from properties file

these key1, key2 and key3 are connection names.

Here is my java code to make an entry into file:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
       try
       {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            con = DriverManager.getConnection("jdbc:oracle:thin:@"+host+":"+port+"/"+service,username,password);
            con.setAutoCommit(false);
            if (con!=null)
            {
                 session.setAttribute(username, con); 
                 out.println("Connected Successfully");          
                 PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("my properties file", true)));
                 out1.println(cname+" = "+host+","+port+","+service+","+username+","+password);
                 out1.close();
            }
            else
            {
                 out.println("Error in getting connection");
            }
       }
       catch(Exception e)
       {
            e.printStackTrace();
       }
}

Upvotes: 1

Views: 1957

Answers (2)

Qwerky
Qwerky

Reputation: 18455

Look at the Properties class. It has some pertinent methods that may be of use;

load()
save()
setProperty()
containsKey()

Upvotes: 0

asifsid88
asifsid88

Reputation: 4701

Properties prop = new Properties();
prop.load("pathToPropertiesFile");


String key; //This is the key which user will enter
String propKey = prop.getProperty(key);
if(propKey == null)
{
    // Key is not present so enter the key into the properties file
      prop.setProperty("keyName", key);
}
else
{
    // Throw error saying key already exists
      out.println("Key "+key+" already exists.");
} 

Refer Here for more information and example on Properties in Java

Updated: Okay, if you wish to check whether such value is present (irrespective) of any key, then use this code

   // Ignoring the loading of the properties file
   // Assuming properties file is loaded in "prop"
   Enumeration keySet = prop.keys();
   String key; // This is the key which user will enter
   boolean keyExists = false;
   while(keySet.hasMoreElements())
   {
       String keyName = (String) keySet.nextElement();
       String keyValue = prop.getProperty(keyName);
       if( key.equals(keyValue))  //Check against all the keys' value present
       {
           keyExists = true;
           break;
       }
   }

   if(keyExists)
   {
       //throw error
   }
   else
   {
       //insert key
   }

The approach is to get all the keys present and check against its values. If the value present in the properties file is same as that user entered or otherwise then you know what is to be done

If you want to make the check against the KeyName then just change the if condition in the loop

if( key.equals(keyName))  //Check against all the key Name present in the properties file
{
 keyExists = true;
 break;
}

Hope this helps!!

Upvotes: 4

Related Questions