如何使用mysql在java中执行result.beforeFirst后使用resultset.next方法

发布于 2024-10-21 01:38:36 字数 1884 浏览 2 评论 0原文

我需要有关如何滚动回 java 返回的结果集上的下一条记录的帮助。我正在使用mysql数据库。

这是 formshow 事件中的代码。我加载返回的第一个结果集的位置:

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"));

             }

这是应该用于向前滚动数据库的按钮。 我需要执行 rs.beforeFirst 因为 jFrame 上显示的内容与我尝试验证的变量不匹配:

   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();
        }

我的问题是如何继续滚动数据库。因为当我在 rs.next() 之前使用 rs.beforeFirst() 时,结果集不会前进 我也尝试这样做:

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

它有效,但它不允许我选择我想要的 radioButton 。即使没有多次手动单击按钮(用于滚动),它也会继续执行,直到结果集结束。请帮我弄清楚这个问题的解决方案是什么。如果您需要更多详细信息,请询问。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-10-28 01:38:36

您不应该将数据库访问逻辑与表示逻辑混合在一起。这只会导致紧密耦合的代码,其中两个问题只会相互冲突。数据库工作需要尽快完成。

您需要将关注点分开。

首先创建一个代表数据库单行的类。

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.
}

(像 Eclipse 这样的像样的 IDE 可以自动生成它们)

然后创建一个执行以下 JDBC 工作的类:

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;
}

最后以简单的方式使用 List

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());
    // ...
}

无需来回按摩结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文