插入新数据后在JcomboBox中更新数据和数据库数据
问题描述:
我在关闭另一个Jframe时无法让JComboBox刷新。插入新数据后在JcomboBox中更新数据和数据库数据
我在之后是组合框从数据库中的数据刷新其数据并显示它。问题是新的数据是通过另一个页面进入的。
例子:
插入了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);
答
如果你想确保邮政编码选择与最新的可用更新邮在客户端创建表单处于打开状态时保存新的邮政编码时,您可以添加一个侦听器到下拉按钮,每次点击按钮。
此外,在创建一个查询字符串这样是不正确:
xpto.executeUpdate("INSERT INTO codigospostais (cod_postal, localidade) VALUES ('" + this.jCodPostal.getText() + "', '" + this.jtflocalidade.getText() +"')");
这使你的代码容易受到SQL注入。尝试阅读PreparedStatements以获得更好的解决方案。
为了更好的帮助,请尽快发布[mcve] – Frakcool