gpt4 book ai didi

java - 在 jdbc 应用程序中向前移动 rs.next()

转载 作者:行者123 更新时间:2023-12-01 18:25:22 25 4
gpt4 key购买 nike

我正在开发一个在线考试软件,该软件使用 JDBC ODBC 连接连接到 Ms Sql 服务器。我想将结果集光标移动到下一行并向后移动。连接正常,程序可以从数据库中检索字段,因此没有问题。

我这里的代码位于标有“提交”的按钮上。当您单击此按钮时,它应该移动到数据库中的下一行并从该行检索数据。检索到的数据应显示在“textarea”中。问题是当我点击下一步时它显示最后的结果?

class Abc extends JFrame implements ActionListener
{

JLabel l1, l2, l3, l4;
JButton b1, b2;
JTextArea j1, j2;
JCheckBox c1, c2, c3, c4;
ButtonGroup group = new ButtonGroup();

JPanel panel2;

Abc ()
{
j1 = new JTextArea();
j2 = new JTextArea();
c1 = new JCheckBox();
c2 = new JCheckBox();
c3 = new JCheckBox();
c4 = new JCheckBox();
j1.setEditable(false);
c1.setSelected(false);
c2.setSelected(false);
c3.setSelected(false);
c4.setSelected(false);

group.add(c1);
group.add(c2);
group.add(c3);
group.add(c4);

Connection conn = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system",
"java");
System.out.println("Dc is obtained");

}
catch (Exception e2)
{
System.out.println("conne" + e2.getMessage());
}
try
{
String sq = "select * from questionans";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sq);
System.out
.println("------------------------------------------");
if (rs.next())
{

j1.setText(rs.getString(2));
c1.setText(rs.getString(3));
c2.setText(rs.getString(4));
c3.setText(rs.getString(5));
c4.setText(rs.getString(6));
}
System.out
.println("-------------------------------------------");
}
catch (Exception e1)
{
System.out.println("query" + e1.getMessage());
}
finally
{
try
{
conn.close();
}
catch (Exception e)
{
System.out.println("close conn" + e.getMessage());
}
}

b1 = new JButton("REGISTER");
b2 = new JButton("SUBMIT");
panel2 = new JPanel(new GridLayout(4, 1));
panel2.add(b1);
panel2.add(j1);
panel2.add(c1);
panel2.add(c2);
panel2.add(c3);
panel2.add(c4);
panel2.add(b2);

add(panel2, BorderLayout.CENTER);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("REGISTRATION");

}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == b1)
{
System.out.println("you pressed button b1");
}
else if (source == b2)
{

Connection conn = null;

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system",
"java");
System.out.println("Dc is obtained");

}
catch (Exception e4)
{
System.out.println("conne" + e4.getMessage());
}
try
{
String sq = "select * from questionans";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sq);
System.out
.println("------------------------------------------");

while (rs.next())
{

j1.setText(rs.getString(2));
c1.setText(rs.getString(3));
c2.setText(rs.getString(4));
c3.setText(rs.getString(5));
c4.setText(rs.getString(6));

}

System.out
.println("-------------------------------------------");
}
catch (Exception e3)
{
System.out.println("query" + e3.getMessage());
}
finally
{
try
{
conn.close();
}
catch (Exception e6)
{
System.out.println("close conn" + e6.getMessage());
}
}

String s2 = "";
if (c1.isSelected())
{
s2 = s2 + "" + c1.getText();
}
else if (c2.isSelected())
{
s2 = s2 + "" + c2.getText();
}
else if (c3.isSelected())
{
s2 = s2 + "" + c3.getText();
}
else if (c4.isSelected())
{
s2 = s2 + "" + c4.getText();
}

System.out.println("You clicked on " + s2);
}
}

}

class xyz
{
public static void main (String args[])
{
Abc a = new Abc();
a.setSize(1000, 1000);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

最佳答案

它显示最后的结果,因为您正在循环所有结果:

while (rs.next())
{
j1.setText(rs.getString(2));
c1.setText(rs.getString(3));
c2.setText(rs.getString(4));
c3.setText(rs.getString(5));
c4.setText(rs.getString(6));
}

应该在监听器外部检索 ResultSet,将这些变量声明为类的公共(public)变量并在构造函数中初始化它们:

String sq = "select * from questionans";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sq);

将 while 语句更改为监听器中的 if :

if(rs.next())
{
j1.setText(rs.getString(2));
c1.setText(rs.getString(3));
c2.setText(rs.getString(4));
c3.setText(rs.getString(5));
c4.setText(rs.getString(6));
}

并且每次点击提交时,都会显示之前检索到的 ResultSet 的下一个结果。

这也会提供更好的性能,因为每次单击提交时都不会执行 select 语句。

关于java - 在 jdbc 应用程序中向前移动 rs.next(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26387444/

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