我想創建一個在線考試項目。當我點擊下一步它應該顯示下一個數據在textarea.but問題是當我點擊提交按鈕它顯示來自數據庫的最新數據(rs.next())打印文本框中的最後一個值。爲什麼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);
}
}
您正在重複重置循環中的UI組件上的文本,因此實際上只保留最後一個值。 – Thilo 2014-10-08 04:37:59
因爲這就是你告訴它'while(rs有更多結果)填寫表單',所以它會顯示最後一行,因爲這是最後一行。可以嘗試'如果(rs.next){...'而不是... – MadProgrammer 2014-10-08 04:38:12
另外,你應該分開你的用戶界面從你的數據庫代碼,並通過某種模型/經理 – MadProgrammer 2014-10-08 04:38:35