1

我想用数据库中新添加或修改的信息刷新 JTable 目前它仅在我单击showDetails或在数据库中插入 1 条记录后才有效。

每次添加或删除新条目时,它只会一次又一次地显示信息

代码属于 GUIClass

package studentmanagementsystem.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import studentmanagementsystem.entity.*;
import studentmanagementsystem.backend.*;

public class StudentPersonalGui extends JFrame implements ActionListener{

JLabel heading,lenroll,lDOB,lgender,lmobileno,lemailid,lfathername,lname;
JLabel lmothername,laddress;
            JTextField tenroll,tDOB,tmobileno,temailid,tfathername,tname;
            JTextField tmothername,taddress;
            JComboBox<String> cgender;
            JButton insert,retrieve;
            JButton reset;
            JPanel p1,dataPanel;
            String gender[]={"Male","Female"};
            Font f1=new Font("BOLD",Font.ITALIC,25);
            Color c1=Color.white;
            Color c2=Color.lightGray ;
            Color c3=new Color(50,100,10);
            boolean insert_status;

            StudentPersonal sp;

                public StudentPersonalGui(){
                        setLayout(null);

                        heading=new JLabel("Student Personal Record");
                        heading.setFont(f1);
                        heading.setForeground(c3);
                        p1=new JPanel();
                        p1.add(heading);
                        p1.setBackground(c2);
                        p1.setLayout(null);
                        p1.setBounds(200,20,400,60);
                        heading.setBounds(50,10,300,30);
                        add(p1);

                        lenroll=new JLabel("Enrollment No.:");
                        lname=new JLabel("Student Name:");
                        lDOB=new JLabel("Date of Birth:");
                        lmobileno=new JLabel("Mobile No.:");
                        lemailid=new JLabel("Email Id:");
                        lgender=new JLabel("Gender:");
                        lfathername=new JLabel("Father Name:");
                        lmothername=new JLabel("Mother Name:");
                        laddress=new JLabel("Address:");


                        tenroll=new JTextField(12);
                        tname=new JTextField(12);
                        tDOB=new JTextField(12);
                        tmobileno=new JTextField(12);
                        temailid=new JTextField(12);
                        tfathername=new JTextField(12);
                        tmothername=new JTextField(12);
                        taddress=new JTextField(12);

                        cgender=new JComboBox<String>(gender);

                        insert=new JButton("Add Record");
                        retrieve=new JButton("View Details");
                        reset=new JButton("Reset");

                        add(lenroll);
                        add(lDOB);
                        add(lmobileno);
                        add(lgender);
                        add(lemailid);
                        add(lfathername);
                        add(lmothername);
                        add(lname);
                        add(laddress);


                        add(tenroll);
                        add(tDOB);
                        add(tmobileno);
                        add(cgender);
                        add(temailid);
                        add(tfathername);
                        add(tmothername);
                        add(tname);
                        add(taddress);


                        add(insert);
                        add(retrieve);
                        add(reset);

                        lenroll.setBounds(100,100,100,20);
                        tenroll.setBounds(220,100,150,20);

                        lname.setBounds(450,100,100,20);
                        tname.setBounds(570,100,200,20);

                        lDOB.setBounds(100,130,100,20);
                        tDOB.setBounds(220,130,120,20);

                        lgender.setBounds(450,130,100,20);
                        cgender.setBounds(570,130,100,20);

                        lmobileno.setBounds(100,160,100,20);
                        tmobileno.setBounds(220,160,120,20);

                        lemailid.setBounds(450,160,200,20);
                        temailid.setBounds(550,160,200,20);

                        lfathername.setBounds(100,190,100,20);
                        tfathername.setBounds(220,190,200,20);

                        lmothername.setBounds(100,220,100,20);
                        tmothername.setBounds(220,220,200,20);

                        laddress.setBounds(100,250,200,20);
                        taddress.setBounds(220,250,350,20);



                        insert.setBounds(150,300,150,20);
                        reset.setBounds(350,300,80,20);
                        retrieve.setBounds(480,300,150,20);

                        insert.addActionListener(this);
                        reset.addActionListener(this);
                        retrieve.addActionListener(this);

                        dataPanel=new JPanel();
                        dataPanel.setBounds(50,350,730,250);
                        dataPanel.setLayout(new BorderLayout());
                        dataPanel.setBackground(Color.WHITE);
                        add(dataPanel);


                        setResizable(false);
                        setVisible(true);
                        setSize(850,700);
                        setTitle("Student Record Management");
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                }

                public void actionPerformed(ActionEvent ae){
                        if(ae.getActionCommand()=="Add Record"){
                                sp=new StudentPersonal();
                                String sDOB=tDOB.getText();
                                String sname=tname.getText();
                                String gender=(String)cgender.getSelectedItem();
                                String emailid=temailid.getText();
                                String fathername=tfathername.getText();
                                String mothername=tmothername.getText();
                                String address=taddress.getText();

                                long enrollno=0;
                                long mobileno=0;
                                try{
                                     enrollno=Long.parseLong(tenroll.getText());
                                     mobileno=Long.parseLong(tmobileno.getText());
                                }catch(NumberFormatException nfe){
                                JOptionPane.showMessageDialog(null,"Alphabets cann't be inserted in phone field","Information", JOptionPane.ERROR_MESSAGE);
                                }
                                sp.setSenrollno(enrollno);
                                sp.setSname(sname);
                                sp.setSDOB(sDOB);
                                sp.setSgender(gender);
                                sp.setSmobileno(mobileno);
                                sp.setSemailid(emailid);
                                sp.setSfathername(fathername);
                                sp.setSmothername(mothername);
                                sp.setSaddress(address);

                                insert_status=new CRUDOperations().insert(sp);
                                if(insert_status){
                                JOptionPane.showMessageDialog(null,"Record Inserted Successfully","Information", JOptionPane.INFORMATION_MESSAGE);
                                showData();
                                }
                        }
                        else if(ae.getActionCommand()=="View Details"){
                            showData();
                        }
                        else{
                            tenroll.setText("");
                            tDOB.setText("");
                            tmobileno.setText("");
                            temailid.setText("");
                            tfathername.setText("");
                            tname.setText("");
                            tmothername.setText("");
                            taddress.setText("");
                            cgender.setSelectedIndex(0);    
                        }
                }

                public void showData(){
                    JTable table;
                    JScrollPane pane;
                    Vector<String> columnname=new Vector<String>();
                    Vector<Vector<String>> rowData=new Vector<Vector<String>>();
                    Vector<String> student;
                    try{
                                ResultSet rs=new CRUDOperations().retrieve_Data();
                                if(rs!=null){
                                    ResultSetMetaData rsmd=rs.getMetaData();
                                        int columnCount=rsmd.getColumnCount();
                                            for(int j=1;j<=columnCount;j++){
                                                columnname.add(rsmd.getColumnName(j));
                                            }
                                        while(rs.next()){
                                            student=new Vector<String>();
                                            student.add(rs.getString(1));
                                            student.add(rs.getString(2));
                                            student.add(rs.getString(3));
                                            student.add(rs.getString(4));
                                            student.add(rs.getString(5));
                                            student.add(rs.getString(6));
                                            student.add(rs.getString(7));
                                            student.add(rs.getString(8));
                                            student.add(rs.getString(9));

                                            rowData.add(student);
                                        }   
                                        rs.close();
                                        table = new JTable(rowData,columnname);
                                        pane=new JScrollPane(table);
                                        dataPanel.add(pane,BorderLayout.CENTER);
                                        add(dataPanel);
                                        validate();
                                        repaint();

                                }
                            }catch(SQLException sqle){
                            }
                }
}

CRUDOperation 类用于在 ShowDetails 按钮上进行 DB 操作单击一个 CRUDOperation 函数retrieve_Data 被调用,它返回用于设置 JTable 数据的结果集

package studentmanagementsystem.backend;

 import java.awt.Color;
 import java.sql.*;
 import java.util.Vector;
 import studentmanagementsystem.entity.*;
 import studentmanagementsystem.gui.*;
  public class CRUDOperations{

    Connection con;
    PreparedStatement stat;
    StudentPersonal sp;
    boolean insert_status,delete_status,update_status,login_status;

    public boolean insert(StudentPersonal sp){
            con=new ConnectionManager().makeConn();
            try{
                long enrollno=sp.getSenrollno();
                String sname=sp.getSname();
                String sDOB=sp.getSDOB();
                String gender=sp.getSgender();
                String emailid=sp.getSemailid();
                long mobileno=sp.getSmobileno();
                String fathername=sp.getSfathername();
                String mothername=sp.getSmothername();
                String address=sp.getSaddress();

                String  insertSP="insert into StudentPersonal values(?,?,?,?,?,?,?,?,?)";
                    stat=con.prepareStatement(insertSP);
                    stat.setLong(1,enrollno);
                    stat.setString(2,sname);
                    stat.setString(3,sDOB);
                    stat.setString(4,gender);
                    stat.setLong(5,mobileno);
                    stat.setString(6,emailid);
                    stat.setString(7,fathername);
                    stat.setString(8,mothername);
                    stat.setString(9,address);


                        int i=stat.executeUpdate();
                        if(i>0){
                            insert_status=true;
                        }
                    }catch(Exception e){
                    }
                    return insert_status;
    }

    public ResultSet retrieve_Data(){
        ResultSet rs=null;
        try{
            con=new ConnectionManager().makeConn();
            String retrieve="select * from StudentPersonal";
            stat=con.prepareStatement(retrieve);
            rs=stat.executeQuery();
        }catch(SQLException sql){
            System.out.println(sql);
        }
        return rs;
    }
}
4

3 回答 3

2

您可以使用TableModel并更新它的内容,而不是为每个事件添加新的 JTable 。

这是如何创建 TableModel的示例

在这里你可以找到另一个JDBC with JTable的链接

于 2012-10-28T09:37:38.960 回答
1

为您的表使用DefaultTableModel,而不是将数据直接添加到表中。只需将数据添加到模型并将此模型设置到您的表中。

DefaultTableModel dtm = new DefaultTableModel(data, columnNames);
JTable table = new JTable();
table.setModel(dtm);

现在,当您想要添加数据时,只需添加到您的模型中......

dtm.addRow(...);
于 2012-10-28T14:18:44.777 回答
1

在表模型中,您应该触发适当的事件,包括表行更改、表列更改、表数据更新等。

于 2012-10-28T08:24:37.083 回答