Table model code style This is my table model code style:

public class MyModel extends DefaultTableModel {

static Vector data = new Vector();
static Vector column = new Vector();
Connection conn;
Statement statement;
ResultSet result;

public MyModel() {
super(data, column);
try {
    conn = DriverManager.getConnection(...);
    statement = conn.createStatement();
    result = statement.executeQuery("Select * from table");

    int col = result.getMetaData().getColumnCount();
    for (int i = 1; i &lt;= col; i++) {
        column.add(result.getMetaData().getColumnName(i));
    }

    while (result.next()) {
        Vector newRow = new Vector(col);
        for (int i = 1; i &lt;= col; i++) {
            newRow.add(result.getString(i));
        }
        data.add(newRow);
    }

} catch (SQLException e) {
    e.printStackTrace();
} 
}
// create methods for remove and add and edit a JTable selected row

}

//My GUI class that show jtable on own


Is this best way?

What is this way problems?

Any idea to be better?
This way your software is highly coupled. I can see three big responsibilities in it:


Get data from db
Format data in "TableModel way"
Create TableModel


Let's create a class to get data. I don't like the idea of let other classes deal with objects from jdbc api, so this class will format data too:

public class TableData {

    public TableContent getData() {
        Vector&lt;String&gt; headers = new Vector&lt;String&gt;();
        Vector&lt;Vector&lt;String&gt;&gt; content = new Vector&lt;Vector&lt;String&gt;&gt;();

        try {
            Connection conn = DriverManager.getConnection("");
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("Select * from table");

            headers = buildHeaders(rs);
            content = buildContent(rs);

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

        return new TableContent(headers, content);
    }

    private Vector&lt;String&gt; buildHeaders(final ResultSet rs) throws SQLException {
        Vector&lt;String&gt; headers = new Vector&lt;String&gt;();

        int col = rs.getMetaData().getColumnCount();
        for (int i = 1; i &lt;= col; i++) {
            headers.add(rs.getMetaData().getColumnName(i));
        }
        return headers;
    }

    private Vector&lt;Vector&lt;String&gt;&gt; buildContent(final ResultSet rs) throws SQLException {
        Vector&lt;Vector&lt;String&gt;&gt; content = new Vector&lt;Vector&lt;String&gt;&gt;();

        while (rs.next()) {
            int col = rs.getMetaData().getColumnCount();
            Vector&lt;String&gt; newRow = new Vector&lt;String&gt;(col);

            for (int i = 1; i &lt;= col; i++) {
                newRow.add(rs.getString(i));
            }
            content.add(newRow);
        }
        return content;
    }
}


I've created TableContent just to wrap the result:

public class TableContent {

    private final Vector&lt;String&gt; headers;
    private final Vector&lt;Vector&lt;String&gt;&gt; content;

    public TableContent(final Vector&lt;String&gt; headers, final Vector&lt;Vector&lt;String&gt;&gt; content) {
        this.headers = headers;
        this.content = content;
    }

    public Vector&lt;String&gt; headers() {
        return headers;
    }

    public Vector&lt;Vector&lt;String&gt;&gt; content() {
        return content;
    }
}


Now, the TableModel. That way you don't need to extend the class DefaultTableModel, you can simply instantiate one:

TableContent data = new TableData().getData();
new DefaultTableModel(data.headers(), data.content());


The class TableData still has a problem, the ideal would be to receive the Connection by the constructor, because without it, it's not possible to test the class with unit tests. I didn't wanna make the solution more difficult.