将行插入数据库

时间:2015-07-04 06:28:45

标签: java mysql swing

只要将一行或多行插入MYSQL数据库,我就会不断更新JTable

我使用JTable方法填充了我的tableModel()数据库中的数据,如果插入了新行,我使用changed()方法更新了我的JTable数据库。

我在fireTableDataChanged()方法的setModel()内尝试了repaint()JTablechange(),但它没有做任何事情。

以下是我的代码:

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.mysql.jdbc.ResultSetMetaData;

@SuppressWarnings("serial")
public class Test extends JPanel{
    private JPanel panel;
    private JTable table;
    private JScrollPane pane;

    public Test() {

        setLayout(new BorderLayout());
        panel = new JPanel(new BorderLayout());
        try {
            table = new JTable(tableModel());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        changed(table);
        pane = new JScrollPane(table);
        panel.add(pane);
        add(panel, BorderLayout.CENTER);
    }

    public DefaultTableModel tableModel() throws SQLException {
        ResultSet rs = null;
        Connection myConn = null;

        try {
            //connect to database
            myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","abcd");

            //Create SQL statement
            Statement stmt = (Statement) myConn.createStatement();

            //Execute Query
            rs = stmt.executeQuery("SELECT field1,field2 FROM test");
            //Process result set

        }
        catch(Exception e) {
            e.printStackTrace();
        }

        ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
        myConn.close();
        DefaultTableModel dtm = new DefaultTableModel(data, columnNames);
        return dtm ;
    }

    public void changed(JTable table) {
        DefaultTableModel dtm = (DefaultTableModel)table.getModel();
        dtm.fireTableDataChanged();
        table.repaint();
        table.setModel(dtm);


    }

    public static void main(String[] args) {    
        JFrame frame = new JFrame();
        frame.add(new Test());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(510,390);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

    }
}

1 个答案:

答案 0 :(得分:1)

在初始加载后,您没有在任何地方更新与JTable相关联的DefaultTableModel。您必须以编程方式更新DefaultTableModel以将更新传播到JTable。例如:

void changed(JTable table) 
{ 
   DefaultTableModel dtm = (DefaultTableModel)table.getModel(); 
   Vector data = dtm.getDataVector(); 
   Vector data2 = (Vector)data.get(0); 
   data2.clear(); 
   data2.add("changed data1"); 
   data2.add("changed data2"); 
   dtm.fireTableDataChanged(); 
   table.repaint(); 
   table.setModel(dtm); 
}

以上代码将反映更改。您可能必须使用uniquqe键(例如在地图中)维护默认表模型中的行数据,然后检索该行数据(这是一个矢量对象),然后使用更新的更改修改向量。这将反映在JTable