如何在不使用向量的情况下在JTable中显示MySQL数据?

时间:2014-05-06 09:17:21

标签: java swing jtable

如何在不使用vector的情况下在JTable中显示MySQL数据?

我正在做Java Swing应用程序,我想从数据库中显示报告。我正在使用JTable来显示数据库中的数据。我尝试过以下代码,但它没有显示数据库中的数据。我的数据库连接也是正确的。

int i;
static Vector<Vector<String>> data = new Vector<Vector<String>>();

String[] columnNames = {"Registration id", "First Name", "Middle Name", "Last Name", "Address", "Dob", "Age", "Date Of Registration", "Register Rfid No."};
public AdmissionReport()
{
    initComponents();

    //this is the model which contain actual body of JTable
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columnNames);
    //jTable1=new JTable(model);
    jTable1 = new JTable();
    jTable1.setModel(model);
    int i1 = jTable1.getRowCount();
    if (i1 > 1) {
        model.removeRow(0);
        i1 = i1 - 1;
    }

    String str = "select rid,fname,mname,lname,address,dob,age,dor,rfidtagdata from schoolrfid.registration";
    Connection cn;
    ResultSet rs;
    Statement st;

    String rid, fname, mname, lname, add, dob, age, dor, rfidtag;

    try {
        // Change the database name, hosty name,
        // port and password as per MySQL installed in your PC.
        cn = DriverManager.getConnection("jdbc:mysql://" + "localhost:3306/schoolrfid", "root", "root");
        st = cn.createStatement();

        rs = st.executeQuery(str);
        System.out.println("connected to database.. ");
        // int i=0;
        while (rs.next()) {

            //Vector <String> d=new Vector<String>();
            rid = (rs.getString("rid"));
            fname = (rs.getString("fname"));
            mname = (rs.getString("mname"));
            lname = (rs.getString("lname"));
            add = (rs.getString("address"));
            dob = (rs.getString("dob"));
            age = (rs.getString("age"));
            dor = (rs.getString("dor"));
            rfidtag = (rs.getString("rfidtagdata"));

            i = 0;
            String[] data = {rid, fname, mname, lname, add, dob, age, dor, rfidtag};

            model.addRow(data);
            i++;

        }
        if (i < 1) {
            JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (i == 1) {
            System.out.println(i + " Record Found");
        } else {
            System.out.println(i + " Records Found");
        }

    } catch (SQLException e) {

        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:2)

TableModel后面的JTable处理表格后面的所有数据。要在表格中添加和删除行,您需要使用DefaultTableModel

使用此模型创建表:

JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));

添加一行:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});

您还可以按removeRow(int row)删除行,这将删除模型中行的行。

可以找到有关DefaultTableModel的完整详细信息here


与MySQL类似,您可以添加如下所示的行

DefaultTableModel memberTable = (DefaultTableModel) table.getModel();
while(rs.next())
{
    memberTable.addRow(new Object[]{rs.getString('rid'),rs.getString('fname'),rs.getString('lname'),rs.getString('address'),.....});
}

另请参阅此答案

https://stackoverflow.com/a/17655017/1575570