我有一个正在开发的应用程序,它将查询数据库并显示结果。该程序还对数据库进行更改(更新、删除、插入)。直到最近我进行了一些更改时,这些功能中的大部分都可以正常工作。现在我得到一个 SQLException,告诉我我违反了外键约束。我查了一下,发现违规是多个表共享数据的结果。有没有办法克服这个问题?如何在不违反约束的情况下进行更新?这是我的更新方法:
InstructorEditorPanel updateEditorPanel = new InstructorEditorPanel();
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.B_NUMBER, updBNumber);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.FIRST_NAME, updFName);
updateEditorPanel.setFieldText(InstructorEditorPanel.FieldTitle.LAST_NAME, updLName);
int result = JOptionPane.showConfirmDialog(null, updateEditorPanel,
"Update Instructor", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
if(result == JOptionPane.OK_OPTION)
{
for (InstructorEditorPanel.FieldTitle fieldTitle : InstructorEditorPanel.FieldTitle
.values()) {
bNum = getBNumber(updateEditorPanel.getFieldText(fieldTitle.values()[0]));
fName = getFirstName(updateEditorPanel.getFieldText(fieldTitle.values()[1]));
lName = getLastName(updateEditorPanel.getFieldText(fieldTitle.values()[2]));
}
try
{
connection = DriverManager.getConnection(URL);
updateInstructor = connection.prepareStatement(
"UPDATE Instructor SET BNUMBER = ?, FIRSTNAME = ?, LASTNAME = ? WHERE BNUMBER = ?");
}catch(SQLException sqlException){
sqlException.printStackTrace();
System.exit(1);
}//end catch
try
{
updateInstructor.setString(1, bNum);
updateInstructor.setString(2, fName);
updateInstructor.setString(3, lName);
updateInstructor.setString(4,mNumber);
updateInstructor.executeUpdate();
}catch(SQLException sqlException){
sqlException.printStackTrace();
}//end of catch
finally
{
close();
}//end
}
Display(panel); }
直到最近我进行了一些更改时,我才使该方法正常工作。我不知道我做了什么,但现在我得到了违反外键约束的异常
这是例外:java.sql.SQLIntegrityConstraintViolationException:表'INSTRUCTOR'上的更新导致违反外键约束'SQL120408141918440'键(1234500000)。该语句已回滚。
数据库信息
CREATE TABLE Instructor (
BNumber varchar(10) NOT NULL,
FirstName varchar(20),
LastName varchar(30),
PRIMARY KEY (BNumber)
);
CREATE TABLE Section (
CRN int NOT NULL,
Term varchar(6) NOT NULL,
SectionNumber varchar(3),
CourseID varchar(9),
Enrollment smallint,
BNumber varchar(10),
PercentResp numeric(5,2),
CONSTRAINT CRN_Term PRIMARY KEY (CRN,Term),
FOREIGN KEY (CourseID) REFERENCES Course (CourseID),
FOREIGN KEY (BNumber) REFERENCES Instructor (BNumber)
);
INSERT INTO Instructor (BNumber, FirstName, LastName)
VALUES
('0000012345','Bill','Smith'),
('0000023456','Sue','Taylor'),
('0000034567','Skilar','Ramsey'),
('1234500000','Sam','Jones'),
('2345600000','Tyson','Quilez');
INSERT INTO Section (CRN, Term, SectionNumber, CourseID, Enrollment,
BNumber, PercentResp)
VALUES
(40035,'201040','02B','CHM2210', 31,'0000034567',100),
(40001,'201040','02B','CGS1000', 27,'0000012345',100),
(40002,'201040','70B','CGS2100', 25,'0000012345',100),
(40003,'201040','71B','CGS2100', 19,'0000012345',100),
(40004,'201040','01B','COP1000', 15,'0000012345',100),
(40030,'201040','01B','BSCC1005',30,'0000023456',100),
(40031,'201040','02B','BSCC1005',25,'0000023456',100),
(40032,'201040','70B','BSCC1005',24,'0000023456',100),
(40000,'201040','01B','CGS1000', 15,'0000012345',100),
(40034,'201040','01B','CHM2210', 27,'0000034567',100);