【问题标题】:INSERT values from resultset and TextField in the same table在同一个表中插入来自结果集和 TextField 的值
【发布时间】:2015-10-24 05:11:46
【问题描述】:

我正在尝试从 JTextField 中获取 JList 上每个项目的 ma​​triculestage_ID 并将它们插入到 employé_stage 表中仅包含两列 stage_IDma​​tricule 作为外键,分别引用表 stageemployés。我希望将 JList 中的所有记录插入到具有相同 stage_ID 的表 employé_stage 我正在使用此代码返回 cannot add or update child row : 外部约束失败

private void addstageActionPerformed(java.awt.event.ActionEvent evt){                                         
 try{
    for (int i = 0; i < stagelist.getModel().getSize(); i++) {
        String item = stagelist.getModel().getElementAt(i).toString();
        String[] items =item.split("  ");
        if(items.length >= 2){

            String sql ="INSERT INTO stage (nature,datedebs,datefs,durée_S,commentaire,stage_ID) values(?,?,?,?,?,?)  ";    
            String sql2="INSERT INTO employé_stage (matricule) Select  matricule from employés  where nom='"+items[0]+"' and prénom='"+items[1]+"' ";  
            String sql3="INSERT INTO employé_stage  (stage_ID) select  LAST(stage_ID)from stage  ";

            ps2 = conn.prepareStatement(sql2);
            ps3 = conn.prepareStatement(sql3);
            ps = conn.prepareStatement(sql); 

            ps.setString(6,stageID.getText());


            ps.execute();
            ps2.execute();
            ps3.execute();
        }
    }   

    } catch(Exception ev){

        JOptionPane.showMessageDialog(null, ev);
    }  
    miseajour_tab();
}                                        

【问题讨论】:

  • 您能否发布完整的错误消息(我在foreign 之后缺少key),以及您的CREATE TABLE 好吗?否则,我只能猜测ps3 忘记为employee_stage 中的NOT NULL matricule 列添加一个值..
  • 您希望sql2sql3 创建一两条记录吗?
  • @schtever 一条记录

标签: java mysql sql insert resultset


【解决方案1】:

您遇到外键异常是因为 sql2sql3 分别插入一条记录,而外键约束要求在每条记录中填充两个字段。

您必须将这两个合并到一个插入语句中,例如:

String sqlboth="INSERT INTO employé_stage (stage_id, matricule) " +
   "( " +
   " select LAST(s.stage_ID), e.matricule from stage s, employés e " +
   " where e.nom='"+items[0]+"' and e.prénom='"+items[1]+"'" +
   ")";

此外,您应该在插入中使用占位符,这样您就不会受到 SQL 注入攻击。

String sqlboth="INSERT INTO employé_stage (stage_id, matricule) " +
   "( " +
   " select LAST(s.stage_ID), e.matricule from stage s, employés e " +
   " where e.nom=? and e.prénom=?" +
   ")";
PreparedStatement psboth = conn.prepareStatement(sqlboth);
psboth.setString(1, items[0]);
psboth.setString(2, items[1]);
psboth.executeUpdate();

【讨论】:

  • 这个查询返回一个Sql Exception : Subquery returns more than 1 row 是不是因为有多行插入?
  • 这可能是因为您的select matricule ... 可能返回不止一行。 employés 表中是否存在重复名称?
  • 该列表包含许多记录,因此很明显会有很多插入但没有重复。我想插入所有具有相同 stage_ID 的员工的所有matricule
  • matricule|stage_ID (52 | 1 ) (63 | 1)(70 | 1) (80 | 1) ....
【解决方案2】:

我尝试了这段代码,它可以工作,但它只插入了 JList 的第一条记录:

  private void addstageActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try{
    for (int i = 0; i < stagelist.getModel().getSize(); i++) {
        String item = stagelist.getModel().getElementAt(i).toString();
        String[] items =item.split("  ");
        if(items.length >= 2){


   String sql2="INSERT INTO employé_stage (stage_id, matricule) " +
                "VALUES ( " +
                " ?, " +
             " (Select matricule from employés where nom='"+items[0]+"' and prénom='"+items[1]+"')" +
               ")";

        ps2 = conn.prepareStatement(sql2);

        ps = conn.prepareStatement(sql); 
        ps2.setString(1,stageID.getText());
        ps.execute();
        ps2.execute();

        }}   

    } catch(Exception ev){

 JOptionPane.showMessageDialog(null, ev);
   }  
    miseajour_tab(); 
    }                  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多