Der Cursor beginnt bei 1, deine for-Schleife bei 0. Du solltest aufpassen, den in einer Datenbank der Index immer bei 1 beginnt, und nicht wie in Java und anderen Programmiersprachen wie üblich ab 0.
Du kannst es auf 2 Arten machen, die eine die du angesprochen hast, die andere wär, AbstractTableModel zu überschreiben, um halt eine eigene Table zu haben, die sich um das ganze kümmert.
So bleibst du dynamisch.
Code:
ResultSet rs = cl.execute(SQL_QUERY);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] column = new String[columnCount];
int rows = 0;
while(rs.next()) {
rows++;
}
for(int i = 1; i <= columnCount; i++) {
column[i - 1] = rsmd.getColumnName(i);
}
Object[][] erg = new Object[rows][columnCount];
rows = 0;
rs.beforeFirst();
while(rs.next()) {
for(int i = 1; i <= columnCount; i++) {
erg[rows][i - 1] = rs.getString(i);
}
rows++;
}
model = new DefaultTableModel(erg, column);
table.setModel(model);
rs.close();
rs = null;
Willst du aber mit einer eigenen Tabelle arbeiten(um sich
sehr viel zu ersparren!), dann schaue dir AbstractTableModel an.
Aber ich zeige dir ein Beispiel, vielleicht findet man es nicht soo oft im Internet:
ResultSetTableModel
Code:
/**
* Our own ResultSetTableModel class. Our Table model works directly with the ResultSet object, which provides easy use and work
* with the JTable.
*
* @author Stefan Stokic / XxharCs
* @version 2.0
*/
public class ResultSetTableModel extends AbstractTableModel {
protected ResultSet rs;
protected int rowCount;
/**
* Constructor which takes the ResultSet object.
*
* @param rs the ResultSet object
* @throws SQLException if, when moving the cursor it fails
*/
public ResultSetTableModel(ResultSet rs) throws SQLException {
this.rs = rs;
rs.afterLast();
rs.previous();
this.rowCount = rs.getRow();
}
/**
* Returns the column count out of the meta data from the ResultSet object.
*/
@Override
public int getColumnCount() {
try {
return rs.getMetaData().getColumnCount();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the row count.
*/
@Override
public int getRowCount() {
return this.rowCount;
}
/**
* Returns the value at the specified row and columnIndex.
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
return rs.getObject(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Sets the specified value at the specified row and columnIndex.
*/
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex + 1);
rs.updateObject(columnIndex + 1, aValue);
rs.updateRow();
} catch (SQLException e) {
//throw new RuntimeException(e);
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
/**
* Returns the column class of the specified columnIndex.
*/
@Override
public Class<?> getColumnClass(int columnIndex) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
String typeName = rsmd.getColumnClassName(columnIndex + 1);
return Class.forName(typeName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the column name of the specified column.
*/
@Override
public String getColumnName(int column) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
String columnName = rsmd.getColumnName(column + 1);
return columnName;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns if the cell is editable at the specified row and columnIndex.
*/
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
return rsmd.isWritable(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Notifies the table that it's data has changed cause of the search query. The search queries return is in the ResultSet object,
* so it's only needed to set the new ResultSet to change the data of the table model.
*
* @param rs the ResultSet object which contains the return of the search query
* @throws SQLException if the search query fails
*/
public void search(ResultSet rs) throws SQLException {
this.rs = rs;
rs.afterLast();
rs.previous();
this.rowCount = rs.getRow();
this.fireTableDataChanged();
}
}
In der Verwendung:
Code:
protected ResultSetTableModel defTableModel;
protected ConnectionLogic conLogic;
...
...
conLogic = new ConnectionLogic(this.hostname, this.username, this.password, this.dbName);
ResultSet rs = conLogic.execute(SQL_QUERY);
defTableModel = new ResultSetTableModel(rs); // creating our table model
table.setModel(defTableModel); // setting our table model to JTable
...
...
// example if we want to search something
/**
* This method gets called when we search something in our search field.
*
* @param searchedText the text we are searching for
*/
public void search(String searchedText) {
try {
ResultSet rs = conLogic.execute(searchedText);
defTableModel.search(rs);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "An error occured while searching in the table!");
}
}