I am having trouble declaring an enumeration for DB field types that I can use across all functions of a particular class. With the following code I get “cannot resolve USERNAME to a variable type”:
public class SQL_access {
public enum DBfields { BLANK, USERNAME, ID, PASSWORD, FIRSTNAME, LASTNAME };
public boolean loginValidate( String username, String password ){
String DBuser, DBpass;
PreparedStatement table = connectToTable( "firstsql", "users");
ResultSet row = table.executeQuery();;
while(row.next()){
DBuser = row.getString(USERNAME);
if(DBuser.equals(username)){
DBpass = row.getString(PASSWORD);
break;
}
}
}
};
You need to reference the enum Type:
DBfields.USERNAME, or statically import the enum like:Also, in your case, this is not enough. You need to pass the column name — a
String— or the column position — anint— to theResultSet:Used like this, you loose the main advantage of enums which is it’s static nature, but it can still be useful in other places in your code if you refer to these values as a “bag”.