gpt4 book ai didi

Java ResultSet next() 和 previous() 按钮不起作用

转载 作者:行者123 更新时间:2023-12-02 13:25:04 26 4
gpt4 key购买 nike

嘿伙计们,我正在尝试滚动浏览数据库中的行。由于某种原因,我的上一个和下一个按钮不起作用。下一个按钮仅显示第一行,上一个按钮不显示任何内容。我的第一个和最后一个按钮可以使用。

private void previousbtnActionPerformed(java.awt.event.ActionEvent evt) {                                            
try
{
con = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery("SELECT * FROM EMPLOYEE");
if (rs.previous())
{
str1 = rs.getString("emp_id");
emp_id.setText(str1);
str2 = rs.getString("emp_fname");
first_name.setText(str2);
str3 = rs.getString("emp_lname");
last_name.setText(str3);
}
else
{
rs.next();
}

con.close();
}
catch (SQLException err)
{
JOptionPane.showMessageDialog(EmployeeGUI.this, err.getMessage());
}
}

private void nextbtnActionPerformed(java.awt.event.ActionEvent evt) {
try
{
con = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("select * from employee");

if (rs.next())
{
str1 = rs.getString("emp_id");
emp_id.setText(str1);
str2 = rs.getString("emp_fname");
first_name.setText(str2);
str3 = rs.getString("emp_lname");
last_name.setText(str3);
}
else
{
rs.previous();
}
}
catch (SQLException err)
{
JOptionPane.showMessageDialog(EmployeeGUI.this, err.getMessage());
}

}

最佳答案

Here's ResultSet 的 javadoc 内容如下:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

因此,当您获取 ResultSet 对象时,它将始终放置在上一行之前,因此 previous() 将始终返回 false.

假设您尝试使用这些按钮实现分页,我建议您查看 MySQL SELECT documentation并使用 limit 获取行,例如:

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

您可以将其与LIMIT i, 1一起使用,其中i将随着每次点击next而递增,并随着每次点击而递减>上一页

关于Java ResultSet next() 和 previous() 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com