0

I have this enum Category ,a Question class, a database where question ,option, category,etc are stored. Question class has its getter() and setter() My question is how I can store a string type data from database in a Category type? rs.getString("Cateogory") returns a string but i want to store this string in a category type obj. how can i do so?

public enum Category {
Geography,History,GK,Science;

}

public class Question  {
private int srno;
private String question;
private String option1;
private String option2;
private String option3;
private String option4;
private String correctAns;
private Category Category;
private Complexity complexity;

public Question() {
}

Class X{
    ResultSet rs=null;
    Statement stmt=null;
    List<Question> quesList=new ArrayList<Question>();
    Question q=new Question();
    Category c=null;
    // some code here .....
    while(rs.next())
    {
               q.setQuestion(rs.getString("Question"));
               q.setOption1(rs.getString("optionA"));
               q.setOption2(rs.getString("optionB"));
               q.setOption3(rs.getString("optionC"));
               q.setOption4(rs.getString("OptionD"));
               // But how do i store a Category type?
               quesList.add(q);
    }
}
2
  • Make Category a Class instead of an Enum... Commented Jun 13, 2015 at 20:21
  • Why? Aren't the category strings in the database exactly the same as the category constants in your enum? Commented Jun 13, 2015 at 20:24

1 Answer 1

0

Every enum has a static valueOf() method, that takes the name of an enum constant ("Geography" for example), and returns the corresponding enum constant (Category.Geography for example).

So you just need

Category category = Category.valueOf(rs.getString(someIndex));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.