9 Replies Latest reply: Jan 3, 2008 2:04 AM by 807601 RSS

    How to read a .properties file

    807601
      Hi All,
      I want to read a .properties file.The values that i read needs to be passed to a function wriiten in java.Can any one help.A sample code snippet will help a lot .Any ptrs to the question are also welcome....

      thanks in advance

      Monali
        • 1. Re: How to read a .properties file
          807601
          http://java.sun.com/docs/books/tutorial/i18n/resbundle/concept.html
          • 2. Re: How to read a .properties file
            807601
            you would use java.util.Properties class
            ...
            Properties p = new Properties();
            p.load(new FileReader("my.properties"));
            ...
            read more here http://java.sun.com/javase/6/docs/api/java/util/Properties.html
            • 3. Re: How to read a .properties file
              807601
              import java.io.*;
              import java.sql.*;
              import java.util.*;
              
              class QueryTest{
                   
                   private static Connection connect() throws Exception{
              
                        InputStream in = QueryTest.class.getResourceAsStream("jdbc.properties");
                        Properties props = new Properties();
                        props.load(in);
                        in.close();
                        String driver = props.getProperty("jdbc.driver");
                        String url = props.getProperty("jdbc.url");
                        String user = props.getProperty("jdbc.user");
                        String pwd = props.getProperty("jdbc.password");
                        Class.forName(driver);
                        return DriverManager.getConnection(url, user, pwd);
                   }
                   
                   public static void main(String[] args) throws Exception{
              
                        Connection con = connect();
                        //Put your code here
                   }
              }
              • 4. Re: How to read a .properties file
                807601
                import java.util.ResourceBundle;
                import java.util.MissingResourceException;

                public class PropertyRead
                {
                private final static String CLASSNAME = "PropertyRead";
                private static String prop;

                /**
                * This method is responsible for fetching the value for the concerned
                * KEY attribute.
                * @param sKey : Name of key to be fetched.
                * @return String
                */
                public static String getProperty(String sKey)
                {
                try
                {
                               //property file name is prop.properties
                ResourceBundle DirResources = ResourceBundle.getBundle("prop");
                prop = DirResources.getString(sKey);
                }
                catch (MissingResourceException mre)
                {
                System.err.print("Error in: "+CLASSNAME+" Error:"+mre);
                }
                return prop;
                } // end of getProperty

                /**
                * This method is responsible for fetching the value for the concerned
                * KEY attribute from the specified property File.
                * @param sKey : Name of key to be fetched.
                * @param sFile :Name of File containing the Keys.
                * @return String
                */
                public static String getProperty(String sKey,String sFile)
                {
                try
                {
                ResourceBundle DirResources = ResourceBundle.getBundle(sFile);
                prop = DirResources.getString(sKey);
                }
                catch (MissingResourceException mre)
                {
                System.err.print("Error in: "+CLASSNAME+" Error:"+mre);
                }
                return prop;
                } // end of getProperty

                } // End of class PropertyRead
                • 5. Re: How to read a .properties file
                  807601
                  Hi marathaWarrior ,

                  Thanks for your reply .I tried executing ur code. but i get the folloeing exception

                  Exception in thread "main" java.lang.NullPointerException
                       at java.lang.Class.forName0(Native Method)
                       at java.lang.Class.forName(Unknown Source)
                       at Generic.QueryTest.connect(QuerryTest.java:26)
                       at Generic.QueryTest.main(QuerryTest.java:33)
                  I have created jdbc.properties file which is as follows

                       url = jdbc:mysql://localhost:3306/mysql
                       user= root
                       password =password
                       driver=com.mysql.jdbc.Driver

                  If i try to print these values i get null as output

                  Edited by: newbie_xml on Jan 2, 2008 10:42 PM
                  • 6. Re: How to read a .properties file
                    807601
                    Could you paste your exact code here?
                    • 7. Re: How to read a .properties file
                      807601
                      To try out I pasted the code given by u in my application as a demo

                           private static Connection connect() throws Exception{
                           InputStream in = QueryTest.class.getResourceAsStream("JDBC.properties");
                                Properties props = new Properties();
                                props.load(in);
                                //in.close();
                                String driver = props.getProperty("JDBC.driver");
                                System.out.println(driver);
                                String url = props.getProperty("JDBC.url");
                                System.out.println(url);
                                String user = props.getProperty("JDBC.user");
                                System.out.println(user);
                                String pwd = props.getProperty("JDBC.password");
                                System.out.println(pwd);
                                in.close();
                                Class.forName(driver);
                                return DriverManager.getConnection(url, user, pwd);
                                
                           }
                           
                           public static void main(String[] args) throws Exception{

                                Connection con = connect();
                                //Put your code here
                           }
                      }

                      JDBC.properties file is

                           url = jdbc:mysql://localhost:3306/mysql
                                user= root
                                     password =password
                                     driver=com.mysql.jdbc.Driver

                      And the error messages are
                      null
                      null
                      null
                      null
                      Exception in thread "main" java.lang.NullPointerException
                           at java.lang.Class.forName0(Native Method)
                           at java.lang.Class.forName(Unknown Source)
                           at Generic.QueryTest.connect(QuerryTest.java:28)
                           at Generic.QueryTest.main(QuerryTest.java:35)
                      • 8. Re: How to read a .properties file
                        EJP
                        Your code:
                                  String driver = props.getProperty("JDBC.driver");
                                  String url = props.getProperty("JDBC.url");
                                  String user = props.getProperty("JDBC.user");
                                  String pwd = props.getProperty("JDBC.password");
                        Your file:
                        url = jdbc:mysql://localhost:3306/mysql
                        user= root
                        password =password
                        driver=com.mysql.jdbc.Driver
                        The properties named in your code don't exist so you get {null, null, null, null}. Four other properties exist.
                        • 9. Re: How to read a .properties file
                          807601
                          It seems names used in the Property file and those used in your code are not matching. So either change property names as
                          follows
                          JDBC.url = jdbc:mysql://localhost:3306/mysql
                          JDBC.user= root
                          JDBC.password =password
                          JDBC.driver=com.mysql.jdbc.Driver
                          or else
                          use following
                          String driver = props.getProperty("driver");
                          System.out.println(driver);
                          String url = props.getProperty("url");
                          System.out.println(url);
                          String user = props.getProperty("user");
                          System.out.println(user);
                          String pwd = props.getProperty("password");
                          System.out.println(pwd);
                          Let me know if still there is any issue