如何将主键的ID填充到外键

时间:2016-03-13 18:40:58

标签: java sql database jdbc

我刚刚学习Java JDBC,我尝试创建一个示例项目。我创建了两个Prepared Statements,我在其中插入了两个查询表。主键的第一个表和外键的第二个表。使用Statement_RETURN.GENERATED_KEYS返回主键的Id,但是在我的外键中它没有填充并且给我一个值为null。

父表

enter image description here

子表

enter image description here

QUERY:

    String insertALLSECTION_LIST = "INSERT INTO ALLSECTIONS_LIST (SECTION_NAME)"
            + "VALUES (?)";
    String insertALLSECTIONS_SETTINGS = "INSERT INTO ALLSECTIONS_SETTINGS (SECTION_POPULIMIT,ROOM_ASSGN,ADVISER_ASSIGNED,SESSION_ASSIGNED,YRLEVEL_ASSGN,SCHOOL_YEAR)"
            + "VALUES(?,?,?,?,?,?)";

准备好的陈述

    ResultSet generatedKeys = null;
    try (Connection myConn = DBUtil.connect())//Connection
    {
            myConn.setAutoCommit(false);//Turn off auto commit
            try (PreparedStatement myPs = myConn.prepareStatement(insertALLSECTION_LIST,Statement.RETURN_GENERATED_KEYS))//Prepared Statement
            {
                myPs.setString(1,inputSectionName);

                myPs.executeUpdate();

                myConn.commit();

                generatedKeys = myPs.getGeneratedKeys();
                if (generatedKeys.next())//Return ID
                {
                    sectionID = generatedKeys.getInt(1);
                }
                else
                {
                    throw new SQLException("No generated section ID returned");
                }

            }//end of try
            finally 
            {
                if (generatedKeys != null) generatedKeys.close();
            }//end of finally
            try (PreparedStatement myPs = myConn.prepareStatement(insertALLSECTIONS_SETTINGS))//Prepared Statement
            {
                myPs.setInt(1,inputStudentLimit);
                myPs.setString(2, inputRoomAssign);
                myPs.setString(3, inputAdviserAssign);
                myPs.setString(4, inputSession);
                myPs.setString(5, inputYearLevel);
                myPs.setString(6, inputSchoolYear);

                myPs.executeUpdate();

                myConn.commit();

                JOptionPane.showMessageDialog(null, "Insert Successful");
            }//end of try
    }//end of try       

第一张表(ALLSECTIONS_LIST)

enter image description here

正如您在此处所见,主键正在填充。

第二张表(ALLSECTIONS_SETTINGS) enter image description here

这里我的SECTION_ID列给出了空值。

但是当我为我的表运行查询时。仍然给我一个空值我的外键。随意评论。感谢。

1 个答案:

答案 0 :(得分:1)

由于您的第一份预备声明而取回自动生成的密钥后,您必须手动将其插入另一个表格。

以下是修改过的查询:

String insertALLSECTION_LIST = "INSERT INTO ALLSECTIONS_LIST (SECTION_NAME)"
        + "VALUES (?)";

String insertALLSECTIONS_SETTINGS = "INSERT INTO ALLSECTIONS_SETTINGS (SECTION_ID,
        SECTION_POPULIMIT,ROOM_ASSGN,ADVISER_ASSIGNED,
        SESSION_ASSIGNED,YRLEVEL_ASSGN,SCHOOL_YEAR)"
        + "VALUES(?,?,?,?,?,?,?)";

以下是代码段:

ResultSet generatedKeys = null;
try (Connection myConn = DBUtil.connect()) {
    myConn.setAutoCommit(false);
    try (PreparedStatement myPs = myConn.prepareStatement(insertALLSECTION_LIST,
      Statement.RETURN_GENERATED_KEYS)) {
        myPs.setString(1,inputSectionName);
        myPs.executeUpdate();

        myConn.commit();
        generatedKeys = myPs.getGeneratedKeys();
        if (generatedKeys.next()) {
            sectionID = generatedKeys.getInt(1);
        } else {
            throw new SQLException("No generated section ID returned");
        }
    } finally {
        if (generatedKeys != null) generatedKeys.close();
    }

    try (PreparedStatement myPs = myConn.prepareStatement(insertALLSECTIONS_SETTINGS)) {
        /* Note : Change Here */
        myPs.setInt(1, sectionID);
        myPs.setInt(2, inputStudentLimit);
        myPs.setString(3, inputRoomAssign);
        myPs.setString(4, inputAdviserAssign);
        myPs.setString(5, inputSession);
        myPs.setString(6, inputYearLevel);
        myPs.setString(7, inputSchoolYear);
        myPs.executeUpdate();

        myConn.commit();
        JOptionPane.showMessageDialog(null, "Insert Successful");
    }
}