如何更新JTable中的数据?(带代码)



我有一个使用Eclipse创建的JTable,并使用Vector对象填充表。我希望在对jtable进行更改后更新数据库(将数据提供给我的jtable)。我真的不知道该怎么做。有什么建议吗?我使用此代码填充JTable:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class provemain2 {

 public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new provemain2();
            }
        });
    }
    private final JButton button;
    private final JTable table;
    private final DefaultTableModel tableModel = new DefaultTableModel();
    public provemain2() throws HeadlessException {
        JFrame one =new JFrame();
        one.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        one.setVisible(true);
        table = new JTable(tableModel);
        one.add(new JScrollPane(table), BorderLayout.CENTER);
        button = new JButton("Load Data");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Void>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        loadData();
                        return null;
                    }
                }.execute();
            }
        });
        one.add(button, BorderLayout.PAGE_START);
        one.setSize(640, 480);
    }
    private void loadData() {
        //LOG.info("START loadData method");
        button.setEnabled(false);
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root","psw");
                Statement stmt = conn.createStatement()) {
            ResultSet rs = stmt.executeQuery("select * from Acquirenti");
            ResultSetMetaData metaData = rs.getMetaData();
            // Names of columns
            Vector<String> columnNames = new Vector<String>();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                columnNames.add(metaData.getColumnName(i));
            }
            // Data of the table
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int i = 1; i <= columnCount; i++) {
                    vector.add(rs.getObject(i));
                }
                data.add(vector);
            }
            tableModel.setDataVector(data, columnNames);
        } catch (Exception e) {
            //LOG.log(Level.SEVERE, "Exception in Load Data", e);
        }
        button.setEnabled(true);
        //LOG.info("END loadData method");
    }
}

这段代码可能会帮助您解决问题,

 Dbconnection obj=new Dbconnection();
       rs=obj.getAllworkers();
        Vector v1=new Vector();
        v1.addElement("ID");
        v1.addElement("NAME");
        v1.addElement("STATE");
        v1.addElement("MOB");
        Vector v3=new Vector();
        try{
        while(rs.next())
        {
            Vector v2=new Vector();
            v2.addElement(rs.getString(8));
            v2.addElement(rs.getString(2));
            v2.addElement(rs.getString(6));
            v2.addElement(rs.getString(7));
        v3.add(v2);
        }
        }catch(Exception e){}
        JTable wrkr_table=new JTable(v3, v1);
        JScrollPane scp=new JScrollPane(wrkr_table);

这里,8,2,6,7是基于查询的适当列号

rs是ResultSet对象

相关内容

  • 没有找到相关文章

最新更新