开发者

How to use resultset.next method after executing result.beforeFirst in java using mysql

开发者 https://www.devze.com 2023-02-15 22:53 出处:网络
I need help on how to scroll back to the next record on the resultset returned by java. I\'m using mysql database.

I need help on how to scroll back to the next record on the resultset returned by java. I'm using mysql database.

Here is the code inside the formshow event. Where I load the first resultset that is being returned:

if (rs.next()) {
                jLabel5.setText(rs.getString("Question"));
                jRadioButton1.setText("A. " + rs.getString("A"));
         开发者_开发百科       jRadioButton2.setText("B. " + rs.getString("B"));
                jRadioButton3.setText("C. " + rs.getString("C"));
                jRadioButton4.setText("D. " + rs.getString("D"));

             }

And here's the button which is supposed to be used to scroll forward through the database. I need to execute rs.beforeFirst because the things that are displayed on the jFrame doesn't match with the variable that I'm trying to validate:

   try {




            rs.beforeFirst();

            if (rs.next()) {


                jLabel5.setText(rs.getString("Question"));
                jRadioButton1.setText("A. " + rs.getString("A"));
                jRadioButton2.setText("B. " + rs.getString("B"));
                jRadioButton3.setText("C. " + rs.getString("C"));
                jRadioButton4.setText("D. " + rs.getString("D"));





    if (jRadioButton1.isSelected()) {

                        rval = jRadioButton1.getText().charAt(0);
                        if (String.valueOf(rval).equalsIgnoreCase(rs.getString("Answer"))) {
                           JOptionPane.showMessageDialog(null, "Correct! Your answer is " +  rval + " answer is: " + rs.getString("Answer"));


                        } else {
                           JOptionPane.showMessageDialog(null, "Wrong! your answer is " + rval + " answer is: " +  rs.getString("Answer"));
                        }

                }

                }

            }catch (Exception e) {
            e.printStackTrace();
        }

My question is how do I continue on scrolling through the database. Because the resultset doesn't progress when I use the rs.beforeFirst() before the rs.next() I also tried doing:

while(rs.next()){...}

It worked but it didn't let me choose what radioButton I want. And it continued to execute until the end of the result set even without manually clicking on the button(for scrolling) multiple times. Please help me figure out what's the solution to this. If you need more details just ask. Thanks.


You shouldn't be mingling database access logic with presentation logic. That only leads to tight coupled code where the both concerns only collides with each other. The database job needs to be done as soon as possible.

You need to separate the concerns.

First create a class which represents a single row of the database.

public class Question {
    private String text;
    private String answer;
    private String optionA;
    private String optionB;
    private String optionC;
    private String optionD;

    // Add/generate c'tors/getters/setters/equals/hashcode and other boilerplate.
}

(a bit decent IDE like Eclipse can autogenerate them)

Then create a class which does the following JDBC job:

public List<Question> list() throws SQLException {
    List<Question> questions = new ArrayList<Question>();
    // ...

    try {
        // ...

        while (resultSet.next()) {
            Question question = new Question();
            question.setText(resultSet.getString("Question"));
            question.setAnswer(resultSet.getString("Answer"));
            question.setOptionA(resultSet.getString("A"));
            question.setOptionB(resultSet.getString("B"));
            question.setOptionC(resultSet.getString("C"));
            question.setOptionD(resultSet.getString("D"));
            questions.add(question);
        }
    } finally {
        // ...
    }        

    return questions;
}

Finally just work with List<Question> the straightforward way.

List<Question> questions = questionDAO.list();
int size = questions.size();
JOptionPane.showMessageDialog(null, "There are " + size + " questions!");

for (Question question : questions) {
    jLabel5.setText(question.getText());
    jRadioButton1.setText("A. " + question.getOptionA());
    jRadioButton2.setText("B. " + question.getOptionB());
    jRadioButton3.setText("C. " + question.getOptionC());
    jRadioButton4.setText("D. " + question.getOptionD());
    // ...
}

No need to massage the resultset forth and back.

0

精彩评论

暂无评论...
验证码 换一张
取 消