我想创建一个在线考试项目。我点击下一步它应该在textarea中显示下一个数据。但问题是当我点击提交按钮时它会显示数据库中的最后一个数据。 while(rs.next())打印文本框中的最后一个值。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.util.*;
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);
}
}
答案 0 :(得分:0)
我不清楚你的问题。无论如何,你将db表中的所有数据循环到最后的数据,并在while循环中设置setText。这意味着循环结束了文本框中的最后一个数据。