Java JDBC preparedStatement.executeUpdate,数据库中无更改(重复kyes)
我有以下代码:Java JDBC preparedStatement.executeUpdate,数据库中无更改(重复kyes),java,mysql,jdbc,Java,Mysql,Jdbc,我有以下代码: for (Buddy buddy : buddyList.getBuddies()) { int friendId = Integer.parseInt(buddyId); //this is valid btw always preparedStatement = connect.prepareStatement("insert into members_friends " + "(id_memb_friend1, id_memb_f
for (Buddy buddy : buddyList.getBuddies())
{
int friendId = Integer.parseInt(buddyId); //this is valid btw always
preparedStatement = connect.prepareStatement("insert into members_friends "
+ "(id_memb_friend1, id_memb_friend2, active) values (?, ?, ?) "
+ "on duplicate key update id_memb_friend1 = values(id_memb_friend1), "
+ "id_memb_friend2 = values(id_memb_friend2), "
+ "id = LAST_INSERT_ID(id)");
// parameters start with 1
preparedStatement.setInt(1, Math.min(userId,friendId)); //sort it a bit
preparedStatement.setInt(2, Math.max(userId,friendId));
preparedStatement.setInt(3, 1); //active friendship
}
try{
//run prepared statement and add all them to database
int returnValue = preparedStatement.executeUpdate();
}
catch(Exception e){
//I print exception here to file
}
这里要发生的是,不会触发异常,但数据库中并没有任何更改,例如,并没有添加新好友,并没有任何更改。返回值=1
若我在重复密钥更新时使用justinsert而不使用,我将得到一个错误,即数据库中已经存在密钥
My table if Meters具有以下索引/键:
索引:唯一的朋友索引
定义:
类型
树
唯一的
对
柱
id_memb_Friends 1 id_memb_Friends 2
索引:主要
定义:
类型
树
唯一的
对
柱
身份证
索引:id\u memb\u friend1
定义:
类型
树
唯一的
不
柱
我是你的朋友
索引:id\u memb\u friend2
定义:
类型
树
唯一的
不
柱
我的朋友2
我真的需要继续执行并在检测到重复密钥后添加新伙伴。这就是我所需要的。我得到的列表最多可以有100条记录,理论上只有很少的新好友,所以我需要跳过所有其他好友,避免再次检查db他们是否已经是好友您是否提交了事务或自动提交?自动提交,因为我没有使用connection.setAutoCommittrue提交任何关于提交规则的内容;在创建PreparedStatement对象之前,是否有太多的唯一索引?Index unique\u friend\u Index防止复制数据这没问题,但最后两个唯一索引id\u memb\u friend1和id\u memb\u friend2阻止用户拥有多个朋友。文档并没有明确说明如果在重复上更新密钥在第二个约束上失败会发生什么,但它似乎在无声中失败。此外,文档一般说来,您应该尽量避免在具有多个唯一索引的表上使用ON-DUPLICATE-KEY-UPDATE子句。