[英]How to populate Id of Primary Key to Foreign Key
I'm just new learning Java JDBC where I try to create a sample project.I created a two Prepared Statements which where I insert two queries for my tables.我刚开始学习 Java JDBC,我尝试在其中创建一个示例项目。我创建了两个准备好的语句,我在其中为我的表插入了两个查询。 1st table for Primary Key and 2nd table for Foreign Key.主键的第一个表和外键的第二个表。 Using Statement_RETURN.GENERATED_KEYS is returning Id for Primary Key but in my Foreign Key its not populating and throwing me a value of null.使用 Statement_RETURN.GENERATED_KEYS 会返回主键的 Id,但在我的外键中它没有填充并抛出一个 null 值。
Parent Table父表
Child Table子表
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(?,?,?,?,?,?)";
Prepared Statements准备好的报表
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
1st Table (ALLSECTIONS_LIST)第一个表 (ALLSECTIONS_LIST)
As you can see here the Primary Key is populating.正如您在此处看到的,主键正在填充。
2nd Table (ALLSECTIONS_SETTINGS)第二个表 (ALLSECTIONS_SETTINGS)
Here where my SECTION_ID Column gives me null value.在这里,我的 SECTION_ID 列给我空值。
But when I run my query for my tables.但是当我对我的表运行查询时。 Still giving me a Null value for my Foreign Key.仍然为我的外键提供 Null 值。 Feel free to comment.随意发表评论。 Thanks.谢谢。
Once you get back the Auto Generated Key(s) as a result of your first Prepared Statement, you'll have to manually insert it into the other table.一旦您作为第一个准备好的语句的结果取回自动生成的密钥,您就必须手动将其插入到另一个表中。
Here are the modified queries:以下是修改后的查询:
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(?,?,?,?,?,?,?)";
Here is the code snippet:这是代码片段:
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.