Well, here is an approach that does it on class initialization. Which is runtime:
public enum States {
MA, NH; // ...
private String description = "Description of " + name() + " not found in database.";
private int rating;
// Static initialization is performed after the enum constants
// are initialized, but can still change *non-final* fields
// in the constants
static {
String sql = "SELECT abbreviation, description, rating "
+"FROM states "
+"WHERE abbreviation IS NOT NULL ";
ResultSet rs;
// Open connection, create statement, execute, retrieve
// result set. IMPORTANT: catch and properly handle all
// checked exceptions, or else you'll get a nasty
// initialization error. OTOH, you may not want your
// application to start if this fails.
while ( rs.next() ) {
String abbreviation = rs.getString(1);
String description = rs.getString(2);
int rating = rs.getInt(3);
States st;
try {
// Get the enum constant that matches the abbreviation.
st = valueOf(abbreviation);
// Set the values in that constant
st.description = description;
st.rating = rating;
} catch ( IllegalArgumentException e ) {
// This exception happens when the abbreviation
// doesn't match any constant. If you don't put
// anything here, such values will be silently
// ignored. If you don't catch, such values will
// throw an initialization Error.
}
}
// Clean up all database-related stuff.
}
// Only getters, no setters, as values are all
// set from database in the static initialization.
public String getDescription() {
return description;
}
public int getRating() {
return rating;
}
}
With this definition, you can use the enum constants in your program, and the values in the description and rating field will be loaded at class initialization from database. Note that I gave a default value to description which will show up if the particular state's abbreviation is not in the database.
But as I said, this is run time. Although not completely impossible, I can see no sense in loading the values from database at compile time, as these values will stay fixed when you use your resulting .class file or jar. When you change values in your database, the values seen by the application will still be the one hard-compiled into the enum. In fact, you won't even need the database to be up to run the application.
But if you insist on doing this for some reason, well, no IDE will support this directly, I suppose. But you could probably write a script that manipulates the text of your enum java file, and use that script in a pre-compile phase in your build tool (maven, ant...). You'll probably need to write your class much like the above, only with the static initializing block empty. You'll need a clean copy outside of your src directory, and run the script so that it fills up the static initialization block with text derived from the database, and writes the result inside your src directory.
In short: not recommended, system/tool dependent, not useful, but also not impossible.
MyEnum currentState = MyEnum.STATES.MAwhere all 50 would be read in from the database at app initialization.STATES.MA, because you only know what the 50 states are at runtime.