2016-03-12 49 views
0

我正在與我的項目只是爲了學術目的,我遇到一個SQL問題。外鍵沒有得到插入ID的值。爲了達到第二範式。我在一張獨立的桌子上分開。並使用SECTION_ID作爲外鍵匹配它們。在這裏我創建了兩個表格。外鍵沒有得到主鍵的Id值打印NULL

1表

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

隨時發表評論。如果我錯過了某些東西可以指導我出錯的地方。謝謝。

+0

我刪除了無關的數據庫標記。隨意爲您真正使用的數據庫添加標籤。 –

+0

只是不小心點擊了錯誤的標籤 – Francisunoxx

回答

1

看起來你假設你的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出來吧..

我會讓你來修改你的第二個PreparedStatement來設置SECTION_ID值當你插入到ALLSECTIONS_SETTINGS列。

+0

是的兄弟正好。我想填充我的ID到我的第二張桌子。起初我以爲我會爲我的第二個PreparedStatement使用第二個Statement.RETURN_GENERATED_KEYS。但是我做了什麼,我只是手動插入。謝謝!爲您投票。請投我的帖子。 – Francisunoxx