I am trying to read from a constants file the key value pair. It works when I directly use the class name and the field name, but does not work when I dynamically construct the Class+field combination. How to negotiate this issue?
The following is my Constants file
Public interface Constants
{
String DEV_SELECT_STATEMENT = “DEV_INT_SQL_SELECT_STATEMENT”
String INT_SELECT_STATEMENT = “DEV_INT_SQL_SELECT_STATEMENT”
}
Query.properties file
DEV_INT_SQL_SELECT_STATEMENT = “SELECT * FROM SOME TABLE”;
JAVA class file //This works
public someClass
{
public someMethod() //This works
{
String sqlStatement = QueryLoader.getStatement(Constants.DEV_SELECT_STATEMENT);
System.out.println("The key is :" + Constants. DEV_SELECT_STATEMENT);
System.out.println(“SqlStatement is : “ + sqlStatement);
}
}
The key is : DEV_INT_SQL_SELECT_STATEMENT
SqlStatement is : SELECT * FROM SOME TABLE
public someClass //This does not work
{
public someMethod(String env) //This does not work
{
String queryKey = “Constants” +env + “_SELECT_STATEMENT “;
System.out.println(“The Key is : “ + queryKey);
String sqlStatement = QueryLoader.getStatement(queryKey);
System.out.println(“SqlStatement is : “ + sqlStatement);
}
The Key is :Constants.DEV_SELECT_STATEMENT //This does not give the value but a string
SqlStatement is : null
Use Reflection:
EDIT: I notice i had done some illegal things (Constants.class.getClass() is not valid)
Any how I have tested the edited code and it Works.