java外键约束异常处理_SQL中的错误:java.sql.SQLIntegrityConstraintViolationException:违反完整性约束:外键没有父级;...

在尝试向accountInfo表中插入新行时遇到完整性约束错误,已确保外键表的数据完整。创建了accounts、bills、viewerAccount和videos表,并在accountInfo表中设置外键约束。当尝试通过makeAccountInfo方法读取并插入数据时,如果查询不到记录,则会插入新行。错误可能源于外键引用不正确或已有数据冲突。
摘要由CSDN通过智能技术生成

我试图在accountInfo表中插入新行,但出现完整性约束错误。我已经准备好将所有内容添加到外键引用的表中,所以我想知道问题可能出在哪里。

private void createTables(){

// To be completed

String accounts = "create table accounts ( "+

" accountID integer,"+

" billingAddress VARCHAR(100),"+

" primary key(accountID)" +

")";

try {

connection.createStatement().executeUpdate(accounts);

String bills = "create table bills ("

+ " billID integer,"

+ " amount integer,"

+ " accountID integer,"

+ " primary key(billID),"

+ " foreign key (accountID) references accounts);";

connection.createStatement().executeUpdate(bills);

String viewerAccount = "create table viewerAccount ("

+ " viewerName CHAR(100),"

+ " accountID integer,"

+ " primary key(viewerName),"

+ " foreign key (accountID) references accounts)";

connection.createStatement().executeUpdate(viewerAccount);

String videos = "create table videos "

+ "(link CHAR(50),"

+ " creatorName VARCHAR(100),"

+ " videoName VARCHAR(100),"

+ " duration integer,"

+ " primary key(link))";

connection.createStatement().executeUpdate(videos);

String accountInfo = "create table accountInformation ("

+ " accountID integer,"

+ " time integer,"

+ " link CHAR(50),"

+ " primary key(accountID,time),"

+ " foreign key (accountID) references accounts,"

+ " foreign key (link) references videos)";

connection.createStatement().executeUpdate(accountInfo);

}

catch (SQLException e) {

e.printStackTrace(System.out);

}

当我尝试使用此方法从纺织品读取数据时,出现错误:

private int makeAccountInfo(String accountID, String time, String link){

int aID = -1;

try{

PreparedStatement pstmt = connection.prepareStatement(

"Select time From accountInformation where accountID = ? AND time = ?;"

);

pstmt.setInt(1, Integer.parseInt(accountID));

pstmt.setInt(2, Integer.parseInt(time));

ResultSet resultSet = pstmt.executeQuery();

if (resultSet.next()) {

// at least 1 row (hopefully one row!) exists. Get the ID

aID = resultSet.getInt("");

}

else{

// no record

// make the new account

PreparedStatement addAccountInfo = connection.prepareStatement(

"insert into accountInformation (accountID, time, link) values (?, ?, ?);"

);

addAccountInfo.setInt(1, Integer.parseInt(accountID));

addAccountInfo.setInt(2, Integer.parseInt(time));

addAccountInfo.setString(3, link);

int numUpdated= addAccountInfo.executeUpdate();

addAccountInfo.close();

resultSet.close();

}

pstmt.close();

}

catch (SQLException e) {

System.out.println("Error in " + accountID + " " + time + " " + link);

e.printStackTrace(System.out);

}

return aID;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值