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. 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.
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)
As you can see here the Primary Key is populating.
2nd Table (ALLSECTIONS_SETTINGS)
Here where my SECTION_ID Column gives me null value.
But when I run my query for my tables. Still giving me a Null value for my Foreign Key. 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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.