如何使用mysql在java中执行result.beforeFirst后使用resultset.next方法
我需要有关如何滚动回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该将数据库访问逻辑与表示逻辑混合在一起。这只会导致紧密耦合的代码,其中两个问题只会相互冲突。数据库工作需要尽快完成。
您需要将关注点分开。
首先创建一个代表数据库单行的类。
(像 Eclipse 这样的像样的 IDE 可以自动生成它们)
然后创建一个执行以下 JDBC 工作的类:
最后以简单的方式使用
List
。无需来回按摩结果。
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.
(a bit decent IDE like Eclipse can autogenerate them)
Then create a class which does the following JDBC job:
Finally just work with
List<Question>
the straightforward way.No need to massage the resultset forth and back.