2016-12-06 127 views
0

我在關閉另一個Jframe時無法讓JComboBox刷新。插入新數據後在JcomboBox中更新數據和數據庫數據

我在之後是組合框從數據庫中的數據刷新其數據並顯示它。問題是新的數據是通過另一個頁面進入的。

例子:

Frame where the Combobox needs to refresh after other frame has inserted data. But it needs to not delete already entred data into the program Frame where the data is inserted to the database.

插入了JFrame的Codigo postais:

LigacaoBD ligaDB = new LigacaoBD(); 
    Connection con = ligaDB.obterLigacao(); 

    String query=null; 
    Statement xpto; 

    try { 
     xpto= con.createStatement(); 




     xpto.executeUpdate("INSERT INTO codigospostais (cod_postal, localidade) VALUES ('" + this.jCodPostal.getText() + "', '" + this.jtflocalidade.getText() +"')"); 
    } 

    catch (SQLException sqle) { 
     JOptionPane.showMessageDialog(null, sqle + "Erro no query"); 
     //System.out.println("Erro no query"); 
    } 

    ligaDB.fecharLigacao(con); 
+1

爲了更好的幫助,請儘快發佈[mcve] – Frakcool

回答

1

如果你想確保郵政編碼選擇與最新的可用更新郵在客戶端創建表單處於打開狀態時保存新的郵政編碼時,您可以添加一個偵聽器到下拉按鈕,每次點擊按鈕。

此外,在創建一個查詢字符串這樣是不正確:

xpto.executeUpdate("INSERT INTO codigospostais (cod_postal, localidade) VALUES ('" + this.jCodPostal.getText() + "', '" + this.jtflocalidade.getText() +"')"); 

這使你的代碼容易受到SQL注入。嘗試閱讀PreparedStatements以獲得更好的解決方案。