外键未获取主键的Id值会打印NULL

时间:2016-03-12 18:04:42

标签: java sql database jdbc

我正在处理我的项目只是出于学术目的而遇到SQL问题。其中外键没有得到插入ID的值。让我达到第二范式。我在一张独立的桌子上分开了。并使用SECTION_ID作为外键匹配它们。在这里我创建了两个表。

第一张表

ALLSECTIONS_LIST

第二张表

enter image description here

消息代码:

String inputSectionName = Section_SectionName_TextField.getText();
int inputStudentLimit = Section_Student_Limit_ComboBox.getSelectedIndex();
String inputRoomAssign =     Section_Student_Limit_ComboBox2.getSelectedItem().toString();
String inputAdviserAssign = Section_Student_Limit_ComboBox1.getSelectedItem().toString();
String inputSession = Section_Session_Settings_ComboBox.getSelectedItem().toString();
String inputYearLevel = Section_Session_Level_ComboBox.getSelectedItem().toString();
String inputSchoolYear = Section_SchooYear_ComboBox.getSelectedItem().toString();

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(?,?,?,?,?,?)";

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

                myPs.executeUpdate();

                myConn.commit();

            }//end of try
            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       
            catch(SQLException e)
            {
                DBUtil.processException(e);
            }//end of catch

当我运行第一个表的查询时,它给出了这样的输出。 enter image description here

但是当我运行第二个表的查询时, SECTION_ID 列会给出一个空值。 enter image description here

随意发表评论。如果我错过了一些指导我的地方。感谢。

1 个答案:

答案 0 :(得分:1)

您好像假设SECTION_ID表中的ALLSECTIONS_SETTINGS列将自动填充插入ALLSECTIONS_LIST表的最后一个主键值。这不会发生。

您需要做的是获取第一个SECTION_IDPreparedStatement列自动生成的值,并将其设置为第二个PreparedStatement

以下是如何修改您的第一个PreparedStatement以获取生成的SECTION_ID

        int sectionId;
        try (PreparedStatement myPs = myConn.prepareStatement(insertALLSECTION_LIST, Statement.RETURN_GENERATED_KEYS))//Prepared Statement
        {
            myPs.setString(1,inputSectionName);

            myPs.executeUpdate();

            myConn.commit();

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

变化是:

  • 添加新变量sectionId以保存生成的部分ID
  • Statement.RETURN_GENERATED_KEYS添加到第一行的prepareStatement调用中。这告诉您的数据库返回SECTION_ID生成的值。
  • 从语句中获取生成的键结果集,并从中读取部分ID ..

当您插入PreparedStatement时,我会立即修改您的第二个SECTION_ID以设置ALLSECTIONS_SETTINGS列的值。

相关问题