<html>
<body>
<pre>
geting a error when click on the update button as : "com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_customer'. Cannot insert duplication key in object 'dbo.Customer'. The duplication key value is ()."
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=77OOP062;user=sa;password=hnd";
Connection conn= DriverManager.getConnection(url);
String value1=jTextFieldCustomerName.getText();
String value2=jTextFieldHomeAddress.getText();
String value3=jTextFieldNIC.getText();
String value4=jTextFieldEmailAddress.getText();
String value5=jTextFieldContactNo.getText();
String value6=jLabel7.getText();
pst=conn.prepareStatement("update Customer set CustomerName=?,HomeAddress=?,NIC=?,EmailAddress=?,ContactNo=?,InvoiceNo=?");
pst.setString(1,value1);
pst.setString(2,value2);
pst.setString(3,value3);
pst.setString(4,value4);
pst.setString(5,value5);
pst.setString(6,value6);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Güncellendi");
}catch(Exception e){;
JOptionPane.showMessageDialog(null, e);
}
distable();
}
</pre>
</body>
</html>
&#13;
在单击更新按钮时发生错误:&#34; com.microsoft.sqlserver.jdbc.SQLServerException:违反PRIMARY KEY约束&#39; PK_customer&#39;。无法在对象&#39; dbo.Customer&#39;中插入重复键。复制键值为()。&#34;
答案 0 :(得分:1)
由于您的update
语句未包含where
子句,因此您的语句会尝试更新表中的所有行,因为您已将主键列{{1}在更新语句中,它也会被覆盖,但在第一次写入后,rdbms意识到你在NIC
列中放入相同的值(由于pk必须是唯一的,因此不允许)你错了。
使用NIC
子句过滤您想要更新的行并且不要更新pk。