【问题标题】:How to insert record in many to many relationship link table如何在多对多关系链接表中插入记录
【发布时间】:2013-01-13 09:43:06
【问题描述】:

目前我正在从事一个医院管理系统项目。我有两张表医生和病人,并在他们之间建立了一个链接表访问。医生表和病人表的主键分别是访问表的外键。我可以从我的代码更新医生表。每当将记录插入患者表时,检查医生表以确定分配的医生是否存在,如果医生存在,则插入访问表。为此,我在患者表上创建了一个触发器。现在,每当我尝试注册患者时,患者表都会更新,但代码会抛出 MySQLException 消息“列计数与第 1 行的值计数不匹配”。

代码

        this.stmt=this.mycon.createStatement();
         String query_add_patient="insert into patients values    ('"+nic+"','"+name+"','"+address+"','"+city+"',"+age+",'"+dob+"','"+telephone+"');";

        String query_select_employees="select * from employees where employee_type='Doctor' and employee_qualification='"+doctor+"'";
        ResultSet rs=this.stmt.executeQuery(query_select_employees);
        if(!rs.next()) {
            JOptionPane.showMessageDialog(this, "This Doctor is not available in this facility!!!","Admin Error",JOptionPane.ERROR_MESSAGE);
            return false;
        }
        rs.first();
        String pattern = "yyyy-m-d";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String enic=rs.getString("employee_nic");
        System.out.println(enic);
        String query="insert into Visits (patient_nic,employee_nic) values ("+nic+","+enic+")";

        this.stmt.executeUpdate(query_add_patient);
        closeConnection();
        openConnection();
        this.stmt=mycon.createStatement();
        this.stmt.executeUpdate(query);
        return true;

这里的“插入访问”语句会导致问题。我试图关闭并再次打开连接,但没有奏效。我的访问表的结构是

访问

  patient_nic (varchar(45)) FK  --> patient.patient_nic
  employee_nic (varchar(45)) FK --> employee-->employee_nic

【问题讨论】:

标签: java mysql jdbc


【解决方案1】:

确保每个列名都有一个匹配的值,反之亦然。

例如

  Good
   $query = "INSERT INTO table (first_name, last_name)
      VALUES(Mr,Kyaw)";
   Bad
   $query = "INSERT INTO table (first_name, last_name)
      VALUES(Mr,Kyaw,Thu)"; //can give Column Count Does Not Match Value Count At Row 1 exception

【讨论】:

  • 感谢大家的建议,但我已经找到了解决方案。我在访问表上放置了一个触发器,错误是由于触发器主体中的插入语句不正确造成的。再次感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多