簡體   English   中英

外鍵未獲取主鍵Id的值將顯示NULL

[英]Foreign Key not getting the value of Id of Primary Key prints NULL

我正在為我遇到SQL問題的學術目的而工作。 凡外鍵未獲取插入ID的值。 對我來說,要達到第二范式。 我分成一個獨立的表。 並使用SECTION_ID作為外鍵進行匹配。 在這里我創建了兩個表。

第一桌

ALLSECTIONS_LIST

第二表

在此處輸入圖片說明

源代碼:

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

當我運行第一張表的查詢時,它會給我這樣的輸出。 在此處輸入圖片說明

但是,當我對第二張表運行查詢時, SECTION_ID列為我提供了空值。 在此處輸入圖片說明

隨時發表評論。 如果我錯過了什么,請指導我哪里出錯了。 謝謝。

似乎您假設ALLSECTIONS_SETTINGS表中的SECTION_ID列將自動填充有插入ALLSECTIONS_LIST表中的最后一個主鍵值。 這不會發生。

您需要做的是獲取第一個PreparedStatementSECTION_ID列自動生成的值,並在第二個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。

當您插入ALLSECTIONS_SETTINGS時,由您自己修改第二個PreparedStatement來設置SECTION_ID列的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM