我的代码显示了SQLINTEGRITYCONSTRAINTSVIOLATION

时间:2019-01-04 10:23:13

标签: java mysql jdbc database-connection

我想在数据库中插入值,我有两个表信息和反馈,其中我创建了一个外键,并将一个JFrame主题和等级中的两个值插入反馈表中,并包含四个属性名称,电子邮件,主题,等级,其中登录时从上一个表中获取名称和电子邮件,并且我的代码显示SQLINTEGRITYCONSTRAINTSVIOLATION。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","harsh");
        String sql = "insert into feedback values(?,'eid','name',?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        String sub;
        sub = subject.getSelectedItem().toString();
        pstmt.setString(1, sub);
        String rate = null;
        if(poor.isSelected()){
            rate = poor.getText();
        }
        if(good.isSelected()){
            rate = good.getText();
        }
        if(excellent.isSelected()){
            rate = excellent.getText();
        }
        pstmt.setString(2, rate);
        pstmt.executeUpdate();

        StudentFeedback sf = new StudentFeedback();
        sf.setVisible(false);
        this.dispose();
        Submitted sm = new Submitted();
        sm.setVisible(true);
        sm.setLocationRelativeTo(null);
        conn.close();



    } catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);

    }
}                        

预期结果给了错误,实际结果是将JFrame中的数据存储在反馈表中。

0 个答案:

没有答案