为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA插入到具有多行的多个表中,具体取决于先前的ID

JAVA插入到具有多行的多个表中,具体取决于先前的ID

呼唤远方 2021-03-30 12:15:28
我正在尝试创建一些内容,可以将锻炼内容中的锻炼内容插入数据库中。问题是我想同时创建它们并存储练习,我使用链接表。该表名为Workout_Excercise,因此我可以在每次锻炼中存储多个练习。问题是希望能够同时创建这些内容,但是execution_excercise需要尚未创建的execution_id。我当前的代码插入的锻炼效果很好,现在很好奇我将如何插入锻炼。当前插入锻炼的代码:正面        $(".insertWorkout").on("click", function(){            if(localStorage.getItem('token') != null ) {                var token = localStorage.getItem('token');             }             else {                 var token = null;             }        $.ajax({             url: '/project/restservices/workouts/insert',             type: 'POST',             beforeSend:  function(xhr){                 xhr.setRequestHeader('Authorization', 'Bearer ' + token);             },             data: {                 titel: $(".workoutTitel").val() ,                 beschrijving: $(".workoutBeschrijving").val(),                 categorie_id: $(".categorieSelect").val(),                 persoon_id: 1             },             success: function(response) {                 if(response){                     $(".workoutTitel").val("");                     $(".workoutBeschrijving").val("");                     $(".messageSpanSucces").html("Oefening invoeren gelukt");                 }                 else {                     $(".messageSpanFail").html("Oefening invoeren mislukt");                 }             }         });    });锻炼资源    @Path("insert")@POST@Produces("application/json")@RolesAllowed("user")public Response saveWorkout(@FormParam("titel") String titel, @FormParam("beschrijving") String beschrijving,        @FormParam("categorie_id") int categorie_id, @FormParam("persoon_id") int persoon_id) throws SQLException {    WorkoutService service = ServiceProvider.getWorkoutService();    boolean country = service.saveWorkout(titel, beschrijving, categorie_id, persoon_id);    return Response.ok(country).build();}
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

您可以使用以下方法来获取生成的密钥:


try (Connection con = super.getConnection()) {

        String query = "INSERT INTO \"Workout\" VALUES(?,?,?,?)";

        String query2 = "INSERT INTO Workout_exercise ... ";

        PreparedStatement pstmt = con.prepareStatement(query,PreparedStatement.RETURN_GENERATED_KEYS);

        pstmt.setString(1, titel);

        pstmt.setString(2, beschrijving);

        pstmt.setInt(3, persoon_id);

        pstmt.setInt(4, categorie_id);

        pstmt.executeUpdate();

        result = true;


        try (ResultSet rs = pstmt.getGeneratedKeys()) {

                    if (rs != null && rs.next()) {

                        workout_pkey = rs.getInt(1);

                        PreparedStatement pstmt2=con.prepareStatement(query2);

                        ...


                        for (Workout_exercise ex: exs) {

                              pstmt2.setInt(1, workout_pkey);

                              ...


                              pstmt2.addBatch();

                        }

                         pstmt2.executeBatch();


           }

       }    

    } catch (SQLException sqle) {

        sqle.printStackTrace();

    }


查看完整回答
反对 回复 2021-04-21
  • 1 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信