gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:14:51 25 4
gpt4 key购买 nike

我正在处理我的项目只是为了学术目的,但我遇到了 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 列给出了一个 null 值。 enter image description here

欢迎评论。如果我错过了一些指导我哪里出错了。谢谢。

最佳答案

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

您需要做的是获取为第一个 PreparedStatement 中的 SECTION_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,以便在插入 ALLSECTIONS_SETTINGS 时设置 SECTION_ID 列的值.

关于java - 外键未获取主键的 Id 值打印 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com