主键重复输入?

时间:2019-10-16 11:27:10

标签: java mysql sql eclipse swing

我正在尝试在我的JAVA代码上更新数据库的值,但是当我尝试这样做时,它告诉我“主键重复的条目(nameOfSomething)”。即使PK不存在,也会给我这个错误,我也不知道为什么。欢迎所有帮助;如果需要更多代码,我会很乐意添加。提前致谢。

/**
SO THE NEW ERROR WOULD BE ON THE IF/ELSE STATEMENT, WHICH IS TRHOWING THE ELSE, THAT SAYS "DATA WAS NOT UPDATED".
*/

JButton btnUpdate = new JButton("Update");
        btnUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String petname = txtName.getText();
                String petphoto = txtPhoto.getText();
                String pettype = txtType.getText();
                String color = txtColor.getText();
                int gender = Integer.parseInt(txtGender.getText());
                int isSterilized = Integer.parseInt(txtSterilized.getText());
                //DATE
                String pedigree = txtPedigree.getText();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                String date_entry = df.format(datePicker.getDate());
                //END
                String vaccine1 = txtVaccine1.getText();
                String vaccine2 = txtVaccine2.getText();
                String vaccine3 = txtVaccine3.getText();

                Connection conn = null;
                PreparedStatement pstmt = null;

                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/refugio","root","2797");
                    pstmt = conn.prepareStatement("UPDATE PETS SET PETPHOTO=?, PETTYPE=?, COLOR=?,GENDER=?,ISSTERILIZED=?,PEDIGREE=?,DATE_ENTRY=?,"
                            + "VACCINE1=?,VACCINE2=?,VACCINE3=? WHERE PETNAME=?"); //NEW CHANGE HERE

                    pstmt.setString(1, petname);
                    pstmt.setString(2, petphoto);
                    pstmt.setString(3, pettype);
                    pstmt.setString(4, color);
                    pstmt.setInt(5, gender);
                    pstmt.setInt(6, isSterilized);
                    pstmt.setString(7, pedigree);
                    pstmt.setString(8, date_entry);
                    pstmt.setString(9, vaccine1);
                    pstmt.setString(10, vaccine2);
                    pstmt.setString(11, vaccine3);
                    int i = pstmt.executeUpdate();

                    if(i>0) {
                        JOptionPane.showMessageDialog(null, "Data was updated");
                    }else { //NEW ERROR WOULD BE HERE
                        JOptionPane.showMessageDialog(null, "Data was not updated");
                    }
                }catch(Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }

            }
        });
        btnUpdate.setBounds(48, 737, 356, 51);
        contentPane.add(btnUpdate);




/**
DATABASE DESCRIPTION:
petName     varchar(15) NO  PRI     
petPhoto    varchar(50) YES         
petType     varchar(10) NO          
color       varchar(15) YES         
gender       tinyint(1) NO          
isSterilized tinyint(1) YES         
pedigree    varchar(15) YES         
date_entry  timestamp   NO          
vaccine1    varchar(15) YES         
vaccine2    varchar(15) YES         
vaccine3    varchar(15) YES     

JUST IN CASE PK IS PETNAME. 
*/

2 个答案:

答案 0 :(得分:0)

看起来您的更新查询缺少条件。如果您在没有条件的情况下运行更新脚本,则将为所有行更新相同的数据。

答案 1 :(得分:0)

您的sql语句缺少where子句,因此导致所有pets都更新为相同的值,并且其中一个值看起来是违反主键约束的主键

尝试将您的sql语句更改为

UPDATE PETS SET PETPHOTO=?, PETTYPE=?, COLOR=?,GENDER=?,ISSTERILIZED=?,PEDIGREE=?,DATE_ENTRY=?,VACCINE1=?,VACCINE2=?,VACCINE3=? WHERE PETNAME=?

这是假设PETNAME是主键吗?否则,还需要做更多的工作来获取要更新的行的ID

更新

随着最近对sql的更新,您必须调整准备好的语句参数。 PETNAME已移至最后一个位置,因此应该位于第11点,其他位置也应相应调整

pstmt.setString(11, petname);
pstmt.setString(1, petphoto);
pstmt.setString(2, pettype);
pstmt.setString(3, color);
pstmt.setInt(4, gender);
pstmt.setInt(5, isSterilized);
pstmt.setString(6, pedigree);
pstmt.setString(7, date_entry);
pstmt.setString(8, vaccine1);
pstmt.setString(9, vaccine2);
pstmt.setString(10, vaccine3);

如果您对查询的更改稍有不同,则可以使用命名参数代替位置参数,这样,如果您更改了该命名参数的位置,则无需重新调整位置:

示例:

"UPDATE PETS SET PETPHOTO=:PETPHOTO, PETTYPE=:PETTYPE"

pstmt.setString("PETPHOTO", petphoto);
pstmt.setString("PETTYPE", pettype);

等等...