java.sql.sqlintegrityconstraintviolationexception错误

6pp0gazn  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(233)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

一小时前关门了。
改进这个问题
我正在进行一个视频竞赛,其用户可以参与竞赛,我处理竞赛、视频、参与和用户表我设法将视频存储在数据库中,但竞赛和视频之间的关系(即参与表)阻止了我必须存储竞赛id的部分,显示java.sql.sqlintegrityconstraintviolationexception错误的表中的用户id和视频id:类participationservice中的字段“concur\u id”不能为空(null)
这里有不同的课程,如果有人能帮助我,我会非常感激,并提前感谢。

private Connection connection;
    private Statement ste;
    private PreparedStatement pst;
    private ResultSet rs;
 private Concour c;
     public void setConcour(Concour c) {
        this.c=c;
    }
    public ParticipationServices() {
        Connection connection = MyConnection.getInstance().getCnx();
    }

    public void create(Participation p, Video v) throws SQLException {
        long id = 0;
        String req;
Concour c =this.c;
        req = "INSERT INTO `video`(`url`, `title`, `publish_date`, `owner`) values (?,?,?,(select id from users where id=?))";
        Connection connection = MyConnection.getInstance().getCnx();

        try {

            pst = connection.prepareStatement(req, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1, v.getUrl());
            pst.setString(2, v.getTitle());
            pst.setTimestamp(3, v.getPublish_date());
            pst.setObject(4, v.getOwner().getId());
            pst.executeUpdate();
            ResultSet rs = pst.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getLong(1);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String req2 = "insert into `participation`(concour_id,video_id,date_participation,user_id) values((select id from concour where id=?),(select id from video where id=?),?,(select id from users where id=60))";
        try {
            pst = connection.prepareStatement(req2);
            System.out.println(p.getDateParticipation());
            pst.setObject(1, p.getConcourId());
            pst.setObject(2,p.getUserId().getId());
            pst.setTimestamp(3, (Timestamp) p.getDateParticipation());
            pst.setObject(3, id);

            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }```

```public class ConcourVParticipateController implements Initializable {

    @FXML
    private AnchorPane participation;
    @FXML
    private Button btn_participate;
    @FXML
    private TextField title;
    @FXML
    private WebView preview;
    @FXML
    private Button btn_cancel;
    @FXML
    private Label participation_date;
    @FXML
    private TextField url;
     private Concour c;
     public void setConcour(Concour c) {
        this.c=c;
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        participation_date.setText(new Timestamp(System.currentTimeMillis()).toString());
    }   

    @FXML
    private void participate(ActionEvent event) throws SQLException {
          String t=title.getText();
        String u=url.getText();
        Timestamp time= new Timestamp(System.currentTimeMillis());
//        UserSession s=  UserSession.instance;
        Users owner=new Users(60, "", "", 0, "test", "test", "test", "test", true, 0, "test");
        Video v=new Video(u, t, time, owner);
        Participation cp=new Participation(c, owner, time, v);
        ParticipationServices ps= new ParticipationServices();

        ps.create(cp, v);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("Participation");
            alert.setHeaderText("Participation Added Succesfully");
            alert.setContentText("Go Back To The Homepage");

            alert.showAndWait();
            Stage stage = (Stage) participation.getScene().getWindow();
             stage.close();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题