You have to create your own table model. See the API documentation here: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html
Basically, getColumnCount() would return your number of columns, getRowCount() the number of rows in your database, getColumnName(int columnIndex) some name for every column (the column name from the database or an arbitrary name, maybe from a constant string array). getColumnClass(int columnIndex) can, in the simple case, return String.class for every column. Then you have to convert every value to string.
getValueAt(int rowIndex, int columnIndex) has to return the value from the database for the given row and column. You should probably pre-load all these values in a 2D array or something like this (but that would be the answer to another question ;) ).
You can ignore the other methods for now, they are for editable tables.
Your code could look something like this:
String[][] tableData = readTableDataFromDatabase();
String columnNames = initColumnNames(); //From DB or constants
TableModel model = new DbTableModel(tableData, columnNames);
class DbTableModel extends AbstractTableModel {
private String[][] tableData;
private String[] columnNames;
public DbTableModel(String[][] tableData, columnNames) {
this.tableData = tableData;
this.columnNames = columnNames;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return tableData[rowIndex][columnIndex];
}
@Override
public int getRowCount() {
return tableData.length;
}
@Override
public int getColumnCount() {
if(tableData.length == 0)
return 0;
return tableData[0].length;
}
@Override
public String getColumnName(int column) {
return columnNames[column]
}
}
This example assumes that you read the data from the database as a 2D array. If you use an O/R-Mapper (like the Java Persistence API - JPA), which I highly recommend, you would probably load a list of Entities. Each entity would contain the data for a table row. Anyway, the table model would not change much. Instead of accessing array values, you would get an entity object from the list and call "get"-Methods on the Entity.
There's more information in the Java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
ResultSetTableModelwhich does all the work for you. Here is an article detailing its usage: oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html. All you have to do is provide an SQL query and a DB-connection