我正在使用嵌入式数据库来查询条目的名称并将其放入JList
. 当程序运行时,列表被很好地填充。
我制作了一个函数,应该初始化和刷新名为populateList()
.
以下是我的代码的相关部分:
public class GUI extends JFrame{
private int maxBankNr;
private BankAccountDAO bankAccountDAO;
private DBManager dbm;
...
public GUI(){
initComponents(); //sets up the Swing GUI
this.dbm = new DBManager();
this.bankAccountDAO = dbm.getBankAccountDAO();
...
populateList();
}
private void populateList(){
updateAll = false; //this seems to stop baAccountListValueChanged from throwing an exception
this.maxBankNr = bankAccountDAO.getMaxBankNr(); //max number of bank accounts in database
BankAccount ba;
DefaultListModel dlm = new DefaultListModel();
baAccountList.setModel(dlm);
for(int i = 1; i <= this.maxBankNr; i++){
ba = bankAccountDAO.getBankAccount(i);
dlm.addElement(ba.getName());
}
updateAll = true;
}
...
private void baRefreshButtonActionPerformed(java.awt.event.ActionEvent evt){
populateList();
}
...
我的问题是populateList()
程序启动时工作正常,但是当它被调用时baRefreshButtonActionPerformed
,它似乎什么都不做。该列表保持完全相同。
我尝试了许多不同的方法,例如使用 Vector、使用JList.setListData()
、重新验证、验证和重新绘制所有相关容器。我也尝试过使用不同类型的ListModel
.
另外,我已经读过调用fireContentsChanged()
应该可以工作,但DefaultListModel
不允许这样做,而且我确信无论如何它都会自动调用。
我花了几个小时寻找修复程序,我访问过的大多数网站都说我尝试过的相同内容,尽管它们都不起作用。
我希望这是足够的信息,如果您需要了解其他信息,请告诉我,谢谢。
编辑 我终于设法解决了这个问题,这让我松了一口气:D
它与我的ListModel完全无关,而与我的BankAccountDAO类无关。这是旧版本:
package com.accounts;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BankAccountDAO {
private String insertBankAccountSQL =
"INSERT INTO accounts.bankaccounts(bankNr, sortCode, accountNumber, balance, interest, details, name) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)";
private String getBankAccountSQL =
"SELECT bankNr, sortCode, accountNumber, balance, interest, details, name " +
"FROM accounts.bankaccounts WHERE bankNr = ?";
private Connection conn = null;
private PreparedStatement pstmt = null;
private int maxBankNr = 0;
public BankAccountDAO(Connection theConn){
this.conn = theConn;
try{
this.pstmt = conn.prepareStatement(getBankAccountSQL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(bankNr) FROM accounts.bankaccounts");
if(rs.next()){
maxBankNr = rs.getInt(1);
}
rs.close();
stmt.close();
}catch(SQLException se){
printSQLException(se);
}
}
public int getMaxBankNr(){
return(this.maxBankNr);
}
public void insertBankAccount(int bankNr, int sortCode, int accountNumber, double balance, double interest, String details, String name){
PreparedStatement ins = null;
try{
ins = conn.prepareStatement(insertBankAccountSQL);
ins.setInt(1, bankNr);
ins.setInt(2, sortCode);
ins.setInt(3, accountNumber);
ins.setDouble(4, balance);
ins.setDouble(5, interest);
ins.setString(6, details);
ins.setString(7, name);
ins.execute();
}catch(SQLException se){
printSQLException(se);
}
}
public BankAccount getBankAccount(int targetBankNr){
BankAccount ba = null;
try{
pstmt.clearParameters();
pstmt = conn.prepareStatement(getBankAccountSQL);
pstmt.setInt(1, targetBankNr);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
int bankNr = rs.getInt("bankNr");
int sortCode = rs.getInt("sortCode");
int accountNumber = rs.getInt("accountNumber");
double balance = rs.getDouble("balance");
double interest = rs.getDouble("interest");
String details = rs.getString("details");
String name = rs.getString("name");
ba = new BankAccount(bankNr, sortCode, accountNumber, balance, interest, details, name);
}
rs.close();
}catch(SQLException se){
printSQLException(se);
}
return ba;
}
private void printSQLException(SQLException se){
while(se != null){
System.out.print("SQLException: State: " + se.getSQLState());
System.out.println("Severity: " + se.getErrorCode());
System.out.println(se.getMessage());
se = se.getNextException();
}
}
}
我已将getMaxBankNr() getter 更改如下:
public int getMaxBankNr(){
try{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(bankNr) FROM accounts.bankaccounts");
if(rs.next()){
maxBankNr = rs.getInt(1);
}
}catch(SQLException e){
printSQLException(e);
}
return(this.maxBankNr);
}
当this.maxBankNr = bankAccountDAO.getMaxBankNr()被调用时,它没有返回更新的maxBankNr。这需要一个新的 SQL 语句来返回正确的值。
任何以与我相同的方式使用带有列表的数据库的人(我注意到很多人在谷歌搜索时遇到与我相同的问题)应该确保 for 循环真的知道循环多少次。
我会将此作为答案发布,但我无法在 8 小时内回答我自己的问题,因为我没有足够的分数。