我有这个枚举类别,一个问题类,一个存储问题,选项,类别等的数据库。 问题类有getter()和setter() 我的问题是我如何在Category类型中存储数据库中的字符串类型数据? rs.getString(“Cateogory”)返回一个字符串,但我想将此字符串存储在类别类型obj中。我怎么能这样做?
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);
}
}
答案 0 :(得分:0)
每个枚举都有一个静态valueOf()
方法,它采用枚举常量的名称(例如"Geography"
),并返回相应的枚举常量(例如Category.Geography
)。 / p>
所以你需要
Category category = Category.valueOf(rs.getString(someIndex));