0
public class UserPage extends JFrame implements ActionListener {

JTable table;
UserModel model;
private JButton addButton;

    public UserPage() {
    UserJDBC userJDBC = new UserJDBC();
    model = new UserModel(userJDBC.getData(), userJDBC.getColumn());
    table = new JTable(model);
    addButton = new JButton("Add User");
    addButton.addActionListener(this);
    add(addButton,BorderLayout.SOUTH);
    add(new JScrollPane(table), BorderLayout.CENTER);
    setVisible(true);
}
    @Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == addButton) addAction();
}

    public void addAction() {
    AddUserDialog UserDialog = new AddUserDialog(this);
    model.addQuery(UserDialog.getfName(), UserDialog.getlName(), UserDialog.getGender(), UserDialog.getDate());
}

添加用户对话框类:

public class AddUserDialog extends JDialog implements ActionListener {

    public AddUserDialog(JFrame owner) {
    super(owner, "New User", true);
    add(createForm(), BorderLayout.CENTER);
    setVisible(true);
    pack();
}

@Override
public void actionPerformed(ActionEvent e) {
     if (e.getSource() == okBtn) {
        okAction();
        this.dispose();
    }
}
    public void okAction() {
    setfName(fNameTf.getText());       // Get First name from text field
    setlName(lNameTf.getText());       // Get Last name from text field
    if (!genderSelection()) {          // Get gender as radio buttons
        return;
    }
    setDate(dateGenerate());          // Get date 
}

型号类:

public class UserModel extends DefaultTableModel {

public UserModel(Vector<Vector<String>> data, Vector<String> column) {
    super(data, column);
}
    public void addQuery(String fname, String lname, String gender, String date) {
    try {
        Connection con;
        Statement statement;
        String query = "insert into UserTable(FirstName,LastName,Gender,Date) " +
                "values ( '" + fname + "' , '" + lname + "' , '" + gender + "', '" + date + "' ) ";
        con = DriverManager.getConnection(...);
        statement = con.createStatement();
        if (statement.execute(query)) {
            UserJDBC userJDBC = new UserJDBC();
            fireTableRowsInserted(userJDBC.getData().size() - 1, userJDBC.getData().size() - 1);
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
}

我的 JDBC 类:

public class UserJDBC {

Vector<String> column = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();
Connection connection = null;
Statement statement;
ResultSet resultSet;

    public Vector<String> getColumn() {
          // get column correctly
    }

    public Vector<Vector<String>> getData() {
    try {
        connection = DriverManager.getConnection(...);
        statement = connection.createStatement();
        resultSet = statement.executeQuery("select * from Table");
        int c = resultSet.getMetaData().getColumnCount();

        while (resultSet.next()) {
            Vector<String> eachRow = new Vector<String>(c);
            for (int i = 1; i <= c; i++) {
                eachRow.add(resultSet.getString(i));
            }
            data.add(eachRow);
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    return data;
}
}

现在当我想添加一个新用户时,我的 GUI 需要刷新以显示新行!

更新:

public void addQuery(String fname, String lname, String gender, String date) {
    try {
        Connection con;
        Statement statement;
        String query = "insert into UserTable(FirstName,LastName,Gender,Date) " +
                "values ( '" + fname + "' , '" + lname + "' , '" + gender + "', '" + date + "' ) ";
        con = DriverManager.getConnection(...);
        statement = con.createStatement();
        if (statement.execute(query)) {
            Object[] addedRow = new Object[]{fname,lname,gender,date};
            Vector addedVector = new Vector();
            addedVector.addElement(addedRow);
            this.addRow(addedRow);        // this?
            this.addRow(addedVector);     // Or this?
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
4

1 回答 1

4

不要打扰,我敢肯定很难向您解释...的一致性,简单的快捷方式

1.st级,用法

private ResultSetTableModel tableModel;
private String driver = "full.name.of.driver";
private String dbName = "databseName";
private String url = "jdbc:Xxx.databaseurl, additonal parameter if needed";
private String query = "SELECT * FROM mytablename";
private JTable resultTable; 

初始化的任何地方

try {
   tableModel = new ResultSetTableModel(driver, url, query);
} catch (SQLException ex) {
   // exception handling or JOptionPane
}
resultTable = new JTable(tableModel);

和/或从 void 或 JButtons Action 刷新 TableModel

private void refreshTable(String query) {
    try {
            tableModel.setQuery(query);
    } catch (SQLException sqlException) {
            JOptionPane.showMessageDialog(null, sqlException.toString(), "SQL Exception", JOptionPane.ERROR_MESSAGE);
    }
}

抽象表模型

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.event.TableModelEvent;
import javax.swing.table.*;
// ResultSet rows and columns are counted from 1 and JTable
// rows and columns are counted from 0. When processing
// ResultSet rows or columns for use in a JTable, it is
// necessary to add 1 to the row or column number to manipulate
// the appropriate ResultSet column (i.e., JTable column 0 is
// ResultSet column 1 and JTable row 0 is ResultSet row 1).

public class ResultSetTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private ResultSetMetaData metaData;
    private int numberOfRows;
    // initialize resultSet and obtain its meta data object determine number of rows

    public ResultSetTableModel(String driver, String url,
            String query) throws SQLException, ClassNotFoundException {
        Class.forName(driver);// load database driver class        
        connection = DriverManager.getConnection(url);// connect to database
        // create Statement to query database
        statement = connection.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        setQuery(query);// set query and execute it
    }

    @Override// get class that represents column type
    public Class<?> getColumnClass(int column) {// determine Java class of column        
        try {
            String className = metaData.getColumnClassName(column + 1);
            return Class.forName(className); // return Class object that represents className
        } // catch SQLExceptions and ClassNotFoundExceptions
        catch (Exception exception) {
            exception.printStackTrace();
        }
        return Object.class;// if problems occur above, assume type Object
    }

    @Override// get number of columns in ResultSet
    public int getColumnCount() {
        try { // determine number of columns
            return metaData.getColumnCount();
        } // catch SQLExceptions and print error message
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return 0; // if problems occur above, return 0 for number of columns
    }

    @Override // get name of a particular column in ResultSet
    public String getColumnName(int column) {
        // determine column name
        try {
            return metaData.getColumnName(column + 1);
        } // catch SQLExceptions and print error message
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return "";// if problems, return empty string for column name
    }

    @Override// return number of rows in ResultSet
    public int getRowCount() {
        return numberOfRows;
    }

    @Override // obtain value in particular row and column
    public Object getValueAt(int row, int column) {
        try {  // obtain a value at specified ResultSet row and column
            resultSet.absolute(row + 1);
            return resultSet.getObject(column + 1);
        } // catch SQLExceptions and print error message
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        fireTableCellUpdated(row, column);
        return "";// if problems, return empty string object
    }

    @Override // close Statement and Connection
    protected void finalize() {
        try { // close Statement and Connection
            statement.close();
            connection.close();
        } // catch SQLExceptions and print error message
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }

    // *******set new database query string*********
    public void setQuery(String query) throws SQLException {
        // specify query and execute it
        resultSet = statement.executeQuery(query);
        // obtain meta data for ResultSet
        metaData = resultSet.getMetaData();
        // determine number of rows in ResultSet
        resultSet.last();                   // move to last row
        numberOfRows = resultSet.getRow();  // get row number
        // notify JTable that model has changed
        fireTableStructureChanged();
        fireTableChanged(null);
    }

    public void tableChanged(TableModelEvent e) {
        fireTableChanged(e);
    }
}

第二。将您的代码与这些基本内容进行比较,例如查看连接(始终打开非常漫长而艰难的操作),这真的不是一个好主意

注意永远不要在 DefaultTableModel 中调用 fireWhatever,这些通知程序已正确实施,您是 Swing 中的 Concurency 问题

于 2013-09-11T19:09:40.023 回答