0

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. it ask for a object vector[][] but i have no clue what to give

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);
    }
}

2 Answers 2

1

I think you need something like this.

Note 1. Also add your columns separate to resultset data. 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. It is similar to ArrayList, but with two differences:

  1. Vector is synchronized.

  2. Vector contains many legacy methods that are not part of the collections framework.

Class Vector Javadoc

I hope this will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

but i need to start the loop at 1, because data.getColumnCount returns the first column as 1... doesnt it? what is a vector, though? looks like an arraylist to me
@hamchi You are right getColumnCount() is 1-based not 0-based. I updated the answer.
1

The line 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

According to the documentation of DefaultTableModel:

This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.