简体   繁体   中英

Iterate Linked List and map with the properties file value

I have country.properties file which have values as follows:

1=USA
91=India
20=Egypt
358=Finland
33=France
679=Fiji

and, have a response class file, which is setting a response from database to display it on JSP file. The value that I am getting from database is in the form of code or integer . I needed to have that value from the database and before setting the response I need to use getProperty(code) and save the String representation of that code into a new list and then pass that list to setResponse . For eg: This is the value I am getting from database:

col1 | col2 | col3 |
 1     helo   done

I needed to show on my JSP page as:

col1 | col2 | col3 |
 USA   helo   done

I was following this tutorial, http://www.mkyong.com/java/java-properties-file-examples/ . but not able to exactly understand how to achieve the same.

This is my DAOImpl where I needed to iterate and save the mapped key-value in a new list and then pass to JSP page

public class CountDAOImpl implements IDataDAO {
    private Connection conn = null;
    private Statement statement = null;
    private ResultSet rs = null;
    private List<String> country_code = new LinkedList<String>();
//created a new list to put mapped string value instead of code   
   private List<String> countryMapValue = new LinkedList<String>();

      public CountResponse getValue(String query) throws Exception {
        CountResponse response = new CountResponse();
        try {
            conn = DBConnection.getInstance().getCpds().getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery(query);
            // Extract data from result set
            while (rs.next()) {
                //Retrieve by column name
                String cc = rs.getString("country_code");
                country_code.add(cc);
                }
                //Setting the Response
                response.setCountry(country_code);
                     } catch (Exception e) {

        }
        return response;
    }

Any help on this would be great, as I am new to programming field.

First of all load the property file by following the tutorial from here

... // loading properties file code here

//load a properties file from class path
prop.load(input);

Then, add the below line to extract the value from the property file given a key in your while loop itself -

Sample example below -

//Retrieve by column name
String cc = rs.getString("country_code");
country_code.add(prop.getProperty(cc));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM