2016-12-06 8 views
0

別のJframeを閉じるときにJComboBoxを更新するのに問題があります。新しいデータを挿入した後にJcomboBoxのデータをデータベースデータで更新する

ComboBoxは、データをデータベースのデータから最新表示して表示します。問題は新しいデータが別のページを介してエンレッドされることです。

例:JFrameのCodigoのpostaisため

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.

インサート:

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をお読みください。

関連する問題