2013-01-13 36 views
0

當前我正在從事醫院管理系統項目。我有兩張醫生和病人表,他們之間已經做了一個鏈接表訪問。醫生和患者表的主鍵分別是訪問表的外鍵。我可以從我的代碼更新醫生表。無論何時將記錄插入患者表格中,都要檢查醫生的表格,以確定是否存在分配的醫生,是否存在醫生並將插入插入到就診表格中。爲此,我在患者表上創建了一個觸發器。現在,無論何時嘗試註冊患者,患者表格都會更新,但代碼會拋出一個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 
+0

你檢查過表的定義和插入的值http://htmlfixit.com/cgi-tutes/tutorial_MySQL_Error_Invalid_Query_Column_Count_Does_Not_Match_Value_Count.php – Hitman47

+0

是的,我已經做到了,但沒有工作!!! –

回答

0

確保列名每有一個匹配的值,反之亦然。

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 
+0

謝謝大家的建議,但我找到了解決方案。 我在訪問表上放置了一個觸發器,錯誤是由於觸發器正文中的插入語句不正確。 再次感謝您的建議。 –

相關問題