简体   繁体   中英

Displaying data from JDBC into a JTable

I've followed some previous advice on this site about displaying database information in tables, however mine still won't work. I have a generated Netbeans Dialog with a table named mainTable, I am setting the Model to a ResultSet that has been taken from the MySql.

EDIT the entire class:

import swing.SwingUtilities;
import javax.java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

swing.table.DefaultTableModel;

public class AddressBookImpl extends AddressBookGui implements ActionListener {
final static AddressBookImpl impl = new AddressBookImpl();
DefaultTableModel defaultTableModel = new DefaultTableModel();
AddressBookGui gui = new AddressBookGui();



public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() { impl.startGUI(); }});
}

public void startGUI(){
    gui.main(null);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setResizable(true);
    this.setTitle("Address Book");
    listeners();
    refreshTable();
}

public DefaultTableModel refreshTable() {
    try{

        DatabaseImpl dbimpl = new DatabaseImpl();
        dbimpl.refreshDatabase();
        ResultSet refreshResult = dbimpl.refreshResult;
        ResultSetMetaData meta = refreshResult.getMetaData();
        int numberOfColumns = meta.getColumnCount();

        while (refreshResult.next()) 
        {
            Object[] rowData = new Object[numberOfColumns];

            for (int i = 0; i < rowData.length; ++i)
            {
                rowData[i] = refreshResult.getObject(i + 1);
            }
            defaultTableModel.addRow(rowData);
        }
        this.defaultTableModel = defaultTableModel;
    } catch (Exception e) {
        e.printStackTrace();
    }

    gui.mainTable.setModel(defaultTableModel);
    return defaultTableModel;
}


public void listeners() {
    quitFileMenuBar.addActionListener(this);
    addButton.addActionListener(this);
    refreshButton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equalsIgnoreCase("Quit")) 
    {
        this.dispose();
    }
    if (e.getActionCommand().equalsIgnoreCase("Add"))
    {
        // Add a new address to the DB
    }
    if (e.getActionCommand().equalsIgnoreCase("About"))
    {

    }
    if (e.getActionCommand().equalsIgnoreCase("Refresh"))
    {
        refreshTable();
        System.out.println("Refreshing........");
    }
}
}

And the DatabaseImpl class:

protected ResultSet refreshDatabase() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url, user, password);
        System.out.println("Connection is created to " + url);

        Statement statement = con.createStatement();
        String selectAllQuery = "Select * from " + table + ";";

        ResultSet refreshResult = statement.executeQuery(selectAllQuery);
//          con.close();
        this.refreshResult = refreshResult;
        return refreshResult;

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return refreshResult;
}

However this just shows a blank area where the table should be, any help?

The GUI class is a bog standard netbeans generated class.

It looks like you're passing the TableModel into the wrong JTable, into one that is not being held by the currently visualized AddressBookGui instance. This won't work (as you're finding out) since setting the JTable of a newly created AddressBookGui instance will have absolutely no effect on the visualized object as while they are instances of the same class, they are independent of each other. The solution is to pass this model into the JTable that is held by the actually visualized AddressBookGui instance. Note a bad solution is to use static fields. Just don't do this.

Edit
I think that your problem is with a convoluted misuse of inheritance and again of setting the model of a non-visualized JTable. I suspect that your line of code gui.main(null) calls a static main method that creates and displays an AddressBookGui instance, and that this guy holds your JFrame. If so, then again this AddressBookGui instance that it is displaying is not the same as that held by the gui variable.

I suggest that you not have AddressBookImpl extend AddressBookGui, but rather have it simply hold a instance of AddressBookGui, and that you not call AddressBookGui's main method but rather have AddressBookImpl be responsible for displaying its AddressBookGui instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM