如何在多对多关系链表中插入记录

时间:2013-01-13 09:43:06

标签: java mysql jdbc

目前我正在开展医院管理系统项目。我有两张桌子医生和病人,他们之间有一个链接表访问。医生和患者表的主键分别是Visits表的外键。我可以从我的代码更新医生表。每当将记录插入患者表中时,检查医生表以查明分配的医生是否存在,医生是否存在并且插入访问表中。为此,我在Patient Table上创建了一个触发器。现在,每当我尝试注册患者时,患者表都会更新,但代码会抛出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

1 个答案:

答案 0 :(得分:0)

确保每个列名都具有匹配值和反之亦然。

E.g

  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