java - 我希望创建一个组合的 Java 和 MySQL 语句,从 MySQL 表中导入主键并将其设置为子表中的辅助键
问题描述
所以这里是演出...
我有三个 MySQL 表,版本 8.0.21:
第一个称为 SIL,列有“id”(主键)、“nume”、“tip”、“descriere”。
其次,Rapoarte(Reports),具有列“id”(主键)、“nume”、“tip”、“descriere”、“SIL”(链接到 SIL 表中的“id”列)。
第三,Fisiere(Files),具有列“id”(主键)、“locatie”、“SILlink”(链接到 SIL 表中的“id”列)、“Raportlink”(链接到 Rapoarte 表中的“id”列)
现在,我基本上要做的是,当第一个表需要更新时,SIL、第二个和第三个表也需要成功更新。
到目前为止,我已经取得了相当成功的成就,除了最后一张表中的最后一列,Fisiere……“Raportlink”列。
执行更新操作时,首先我完全删除“Fisiere”表中与要修改的“SIL”关联的所有文件[fisierManagement.deleteBySil(selecteazaSIL.getId());]
,然后重新插入它们,使用全新的“id”使用.save(Fisier实体);增强 for 循环内的方法...
“SILlink”列已成功更新。
然而,“Raportlink”列首先设置为“0”,然后设置为“null”,这实际上意味着该列没有从“Rapoarte”表中的“id”列导入正确的值......是我真正需要它做的!
这是我的代码:
FisierDAO fisierManagement = new FisierDAO(db.getConnection());
SILDAO SILmag = new SILDAO(db.getConnection());
SIL selecteazaSIL = null;
SIL modificaSIL = null;
RaportDAO raportMag = new RaportDAO(db.getConnection());
Raport selecteazaRaport = null;
Raport modificaRaport = null;
List<File> dosarModCom = new ArrayList<>();
private void jBtnModificareComActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnModificareComActionPerformed
// TODO add your handling code here:
try {
if(jFileChooserModCom.getCurrentDirectory().getName().toString().equals(selecteazaSIL.getNume().toString())) {
fisierManagement.deleteBySil(selecteazaSIL.getId());
modificaSIL = SILmag.getSilDupaNume(jFileChooserModCom.getCurrentDirectory().getName().toString());
modificaSIL.setNume(jTextFieldNumeModCom.getText());
modificaSIL.setTip(jTextFieldTipModCom.getText());
modificaSIL.setDescriere(jTextAreaDescModCom.getText());
SILmag.update(modificaSIL);
jFileChooserModCom.getCurrentDirectory().renameTo(new File(jFileChooserModCom.getCurrentDirectory().getParent() +
"\\" + jTextFieldNumeModCom.getText()));
modificaRaport = new Raport(jTextFieldNumeModCom.getText(), jTextFieldTipModCom.getText(), jTextAreaDescModCom.getText(), modificaSIL.getId());
raportMag.updateBySIL(modificaRaport);
scoateSQL_fkConstr();
for(File fisiere : dosarModCom) {
fisierManagement.save(new Fisier(siluriNoi + jTextFieldNumeModCom.getText() +
"\\" + fisiere.getName().toString(), modificaSIL.getId(),
modificaRaport.getId()));
fisierManagement.updateRaportLink(modificaRaport);
}
reinitiereSQL_fkConstr();
jLabelAvertismenteModCom.setText("<html><center>Comanda " + modificaSIL.getNume() + " <br>a fost modificata cu succes!</center><html/>");
jFileChooserVizCom.rescanCurrentDirectory();
} else {
jLabelAvertismenteModCom.setText("<html><center>Doar comanda selectata<br> poate fi modificata!</center></html>");
}
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(MeniuTaburi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch(NullPointerException ex) {
jLabelAvertismenteModCom.setText("<html><center>Nu ai selectat nici o comanda!</center></html>");
}
}//GEN-LAST:event_jBtnModificareComActionPerformed
private void scoateSQL_fkConstr() {
try {
PreparedStatement stmt = db.getConnection().prepareStatement("SET FOREIGN_KEY_CHECKS = 0;");
stmt.execute();
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(DLMAppStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
private void reinitiereSQL_fkConstr() {
try {
PreparedStatement stmt = db.getConnection().prepareStatement("SET FOREIGN_KEY_CHECKS = 1;");
stmt.execute();
} catch (SQLException ex) {
java.util.logging.Logger.getLogger(DLMAppStart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}
public int updateRaportLink(Raport entity) throws SQLException {
PreparedStatement stmt = conn.prepareStatement("UPDATE fisiere SET fisiere.Raportlink = " +
"(SELECT rapoarte.id FROM rapoarte WHERE rapoarte.id = ?);");
stmt.setLong(1, entity.getId());
return stmt.executeUpdate();
}
请记住,“Fisiere”表仅将文件的路径存储为“locatie”列中的 varchar(255),而不是 BLOB 对象!
任何人都可以并且愿意帮助我吗?
解决方案
推荐阅读
- laravel - 添加求和行 - 使用 Livewire (Laravel)
- tree - 如何在不使用子节点的情况下访问 ANTLR 树的节点
- powershell - 如何使用 PowerShell privateMomerySet 和 PID 传递服务路径作为参数?
- c# - Selenium c#单击没有名称或ID的元素
- java - setOnClickListener 使应用程序崩溃 - Android
- r - R中所有国家的ISO-3
- javascript - 关于 JavaScript 中的 return 语句的混淆
- javascript - 即使在页面重新加载后也显示服务工作者更新通知
- shopify - 在 Shopify 产品页面上选择一种颜色选项时隐藏其他产品图片
- excel - 如何将表格从一张纸移动到另一张纸,同时将其重新排列为特定格式?