简体   繁体   English

将带有数据的行从Mysql表添加到jtable

[英]adding rows with data from a Mysql table to jtable

Adding the colums works, but i am stuck when i want to add the data of the columns stored in a mysql database to the jtable . 添加列是可行的,但是当我想将存储在mysql数据库中的列的数据添加到jtable时,我陷入了jtable it ask for a object vector[][] but i have no clue what to give 它要求一个对象vector[][]但我不知道该给什么

Connection con;
DefaultTableModel model = new DefaultTableModel();

public Hoofdscherm() {
    initComponents();
    uitvoerSpelers.setModel(model);

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost/fullhouse", "root", "hamchi50985");

        // selecteer gegevens uit fullhouse.speler tabel
        PreparedStatement stat = con.prepareStatement("SELECT * FROM fullhouse.speler");

        // sla deze GEGEVENS op in een resultset
        ResultSet resultaat = stat.executeQuery();

        // haal alle kolomnamen op PUUR VOOR DE MODEL VAN JTABLE 
        ResultSetMetaData data = resultaat.getMetaData();

        String[] colum = new String[15];
        for (int i = 1; i < data.getColumnCount(); i++) {
            colum[i] = data.getColumnName(i);
            model.addColumn(colum[i]);
            while (resultaat.next()) {
                Object[] gegevens  = new String[] {resultaat.getString(1)};
                model.addRow(gegevens[0]);
            }
        }    
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

I think you need something like this. 我认为您需要这样的东西。

Note 1. Also add your columns separate to resultset data. 注意 1.还将列分别添加到结果集数据。 Like I showed in my code below. 就像我在下面的代码中所示。

Vector<String> rowOne = new Vector<String>();
rowOne.addElement("R1C1");
rowOne.addElement("R1C2");

Vector<String> rowTwo = new Vector<String>();
rowTwo.addElement("R2C1");
rowTwo.addElement("R2C2");

Vector<String> cols = new Vector<String>();

Vector<Vector> vecRow = new Vector<Vector>();
vecRow.addElement(rowOne);
vecRow.addElement(rowTwo);
cols.addElement("Col1");
cols.addElement("Col2");
JTable table = new JTable(vecRow, cols);

Edit 编辑

For you convenience and requirement You can follow code structure below. 为了您的方便和要求,您可以按照以下代码结构进行操作。

 Vector<String> rows = new Vector<String>();
 Vector<Vector> dBdata = new Vector<Vector>();

// Add Columns to table
for (int i = 1; i < data.getColumnCount(); i++) {
    colum[i] = data.getColumnName(i);
    model.addColumn(colum[i]);
}

while (resultaat.next()) {
    // add column data to rows vector
            // Make sure that all data type is in string because of generics
    rows.add(resultaat.getString("columnName1"));
    rows.add(resultaat.getString("columnName2"));
    rows.add(resultaat.getString("columnName3"));

    // add whole row vector to dBdata vector
    dBdata.addElement(rows);
}
model.addRow(dBdata);

Vector implements a dynamic array. Vector实现了动态数组。 It is similar to ArrayList, but with two differences: 它类似于ArrayList,但是有两个区别:

  1. Vector is synchronized. 向量已同步。

  2. Vector contains many legacy methods that are not part of the collections framework. Vector包含许多不属于集合框架的遗留方法。

Class Vector Javadoc 类Vector Javadoc

I hope this will help you. 我希望这能帮到您。

The line model.addRow(gegevens[0]); model.addRow(gegevens[0]); is incorrect. 是不正确的。 You should do something like this: 您应该执行以下操作:

   String[] colum = new String[15];
    for (int i = 1; i < data.getColumnCount(); i++) {
        colum[i] = data.getColumnName(i);
        model.addColumn(colum[i]);
        while (resultaat.next()) {
            Object[] gegevens  = new String[] {resultaat.getString(1)};
            model.addRow(gegevens);
        }
    } 

Also you need to check DefaultTableModel 另外你需要检查DefaultTableModel

According to the documentation of DefaultTableModel: 根据DefaultTableModel的文档:

This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. 这是TableModel的实现,该实现使用Vector的Vector来存储单元格值对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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