我使用这 3 个类将数据库中的数据显示到JTable
.
public class TableContent {
private final Vector<String> headers;
private final Vector<Vector<String>> content;
public TableContent(final Vector<String> headers, final Vector<Vector<String>> content) {
this.headers = headers;
this.content = content;
}
public Vector<String> headers() {
return headers;
}
public Vector<Vector<String>> content() {
return content;
}
和:
public class TableData {
public TableContent getData() {
Vector<String> headers = new Vector<String>();
Vector<Vector<String>> content = new Vector<Vector<String>>();
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<String> buildHeaders(final ResultSet rs) throws SQLException {
Vector<String> headers = new Vector<String>();
int col = rs.getMetaData().getColumnCount();
for (int i = 1; i <= col; i++) {
headers.add(rs.getMetaData().getColumnName(i));
}
return headers;
}
private Vector<Vector<String>> buildContent(final ResultSet rs) throws SQLException {
Vector<Vector<String>> content = new Vector<Vector<String>>();
while (rs.next()) {
int col = rs.getMetaData().getColumnCount();
Vector<String> newRow = new Vector<String>(col);
for (int i = 1; i <= col; i++) {
newRow.add(rs.getString(i));
}
content.add(newRow);
}
return content;
}
}
}
和:
public class TableGUI extends JFrame {
// Create GUI and Show table
}
以前我将我的方法添加到我的扩展 DefaultTableModel 类中,现在我该如何定义这些方法以及应该在哪里做呢?
更新:
例如,我测试了这个方法:
public class TableContent {
String dbUrl = "...";
private final Vector<String> headers;
private final Vector<Vector<String>> content;
public TableContent(final Vector<String> hdr, final Vector<Vector<String>> cnt) {
headers = hdr;
content = cnt;
}
public Vector<String> headers() {
return headers;
}
public Vector<Vector<String>> content() {
return content;
}
public void removeRow(int modelRow, Object rowID) {
String removeQuery = "delete from table where id=?";
Connection conn;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection(...);
ps = conn.prepareStatement(removeQuery);
Object idValue = rowID;
ps.setObject(1, idValue);
if (ps.executeUpdate() == 1) {
content.remove(modelRow);
fireTableRowsDeleted(modelRow, modelRow); // Error
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
现在在这里,问题是fireTableRowsDeleted(...);
!
问题意味着 IDE 说cannot resolve method
fireTableRowsDeleted(int,int)`