我试图将输入关系INPUTRel中的元组插入到具有外键的源关系UK_RD_Spend中,然后更新目标关系UK_Status的元组。 UK_Status和UK_RD_Spend具有主键 - 外键关系。
以下是参数: INPUTRel:TransId,Company,ICB_Code,RD2008 UK_RD_Spend:Company,ICB_Code,RD_spend_2008 UK_Status:公司:RD08
所以我从INPUTRel插入一个元组到UK_RD_Spend。它成功了。 但是当我尝试更新UK_Status的元组时,它返回了SQLException。
我正在尝试更新RD08,但我还需要检查是否违反了外键约束(如果公司名称在UK_RD_Spend中不存在)。但是,我收到了这个错误:
SQLException: UCAExc:::4.0.3 user lacks privilege or object not found:
我不确定如何修复它。我的代码部分如下:
String queryFK = "SELECT * FROM UK_Status WHERE Company = "
+ company + "";
pstmt = conn.prepareStatement(queryFK);
pstmt.setString(1, company);
pstmt.setDouble(2, RD2008);
String msg1;
try {
pstmt.executeUpdate();
} catch (SQLException se) {
// Check FK Violation: company must exist
if (t2.get(company).getCompany() == null) {
// or this ?->
// if
// (!(conn.createStatement()).executeQuery(queryFK).next())
// {
String ICB_CodeTest = Integer.toString(ICB_Code);
String RD2008Test = Double.toString(RD2008);
ICB_CodeTest = ICB_CodeTest.replaceAll("\\s+", "");
RD2008Test = RD2008Test.replaceAll("\\s+", "");
msg1 = "Transaction "
+ tid
+ " failed due to a FK constraint, for the following values of the input; ("
+ company + ", " + ICB_Code + ", " + RD2008
+ ")";
String fk2 = "INSERT INTO OUTPUTRel"
+ "(TransId, RunMessages) " + "values (?, ?)";
pstmt = conn.prepareStatement(fk2);
pstmt.setInt(1, tid);
pstmt.setString(2, msg1);
pstmt.execute();
pstmt.close();
continue;
}
}
//
String companyName = t2.get(company).getCompany();
Status s = t2.get(companyName);
s.setRD2008(RD2008);
String query = "UPDATE UK_Status SET RD08 = ? WHERE Company = ?";
pstmt = conn.prepareStatement(query);
pstmt.setDouble(1, s.getRD2008());
pstmt.setString(2, company);
int number = (int) s.getRD2008();
if (number < ICB_Code) {
msg = "Transaction "
+ tid
+ " succeeded for the following values of the input; ("
+ company + ", " + ICB_Code + ", " + RD2008 + ")";
pstmt.execute();
// Finally insert execution result on the output table.
String oo = "INSERT INTO OUTPUTRel"
+ "(TransId, RunMessages) " + "values (?, ?)";
pstmt = conn.prepareStatement(oo);
pstmt.setInt(1, tid);
pstmt.setString(2, msg);
pstmt.execute();
pstmt.close();
} else {
msg = "Transaction "
+ tid
+ " failed due to a dynamic constraint, for the following values of the input; ("
+ company + ", " + ICB_Code + ", " + RD2008 + ")";
// Finally insert execution result on the output table.
String dynamicFail = "INSERT INTO OUTPUTRel"
+ "(TransId, RunMessages) " + "values (?, ?)";
pstmt = conn.prepareStatement(dynamicFail);
pstmt.setInt(1, tid);
pstmt.setString(2, msg);
pstmt.execute();
pstmt.close();
}
}
答案 0 :(得分:1)
String queryFK = "SELECT * FROM UK_Status WHERE Company = "
+ company + "";
pstmt = conn.prepareStatement(queryFK);
pstmt.setString(1, company);
pstmt.setDouble(2, RD2008);
String msg1;
try {
pstmt.executeUpdate();
} catch (SQLException se) {
您的陈述不能用作PreparedStatement,因为您输入的值代替?
。错误来自BTW,因为您没有引用该值,因此数据库会查找具有公司名称的列,而不是查找值为{{1}的行在“公司&#39;”栏目中。
此外,您使用company
执行该语句,该语句不能与select语句一起使用,也不会返回您可以迭代的结果集。