JTable이 있습니다. 그리고 나는 이와 같은 방법으로 컬럼을 추가했습니다. JTable 채우기시 반복되는 값
private void createSearchResultTable() {
DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
String columnNames[] = {"Title", "Author", "Edition", "Availability", "Reserve"};
for (int i = 0; i < columnNames.length; i++) {
TableColumn column = new TableColumn();
column.setHeaderValue(columnNames[i]);
columnModel.addColumn(column);
}
tblBookSearchResults.setColumnModel(columnModel);
ButtonColumn buttonColumn = new ButtonColumn(tblBookSearchResults, reserveBook, 4);
buttonColumn.setMnemonic(KeyEvent.VK_ENTER);
}
는 지금은 MySQL 데이터베이스에서 검색 한 데이터로의 JTable을 채우는거야.
private boolean populateSearchResultTable(String title, String author, String publisher) {
con = DatabaseHandler.connectToDb();
try {
if (title.trim().length() != 0) {
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE title LIKE ? ");
pst.setString(1, "%" + title + "%");
}
else if (author.trim().length() != 0) {
// Say, this query is getting executed
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE author LIKE ? ");
//pst.setString(1, "%" + author + "%");
pst.setString(1, "Dan");
}
else if (publisher.trim().length() != 0) {
pst = con.prepareStatement("SELECT title, author, edition, status FROM book WHERE publisher LIKE ? ");
pst.setString(1, "%" + publisher + "%");
}
rs = pst.executeQuery();
int rowNum = 0;
while (rs.next()) {
tblBookSearchResults.setValueAt(rs.getString(1), rowNum, 1);
}
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
}
finally {
}
return false;
}
데이터 세트가 문제없이 검색되지만 값을 JTable로 설정하면 다음과 같이 표시됩니다.
첫 번째 값은 모든 열이 반복됩니다. 왜 이런 일이 일어나는지 알 수 없습니까? 이 문제를 해결하는 방법에 대한 제안은 감사하겠습니다.
감사합니다.
더 나은 도움을 더 빨리 얻으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 일부 데이터를 하드 코딩합니다. –