Java preparedstatement批量插入和更新?

Java preparedstatement批量插入和更新?,java,sql,prepared-statement,insert-update,Java,Sql,Prepared Statement,Insert Update,我有query=“select*from user\u message,其中username=”john777@gmail.com“;”。有一列“is_read”(当消息发送给用户时,is_read默认为false,这意味着用户尚未阅读消息),在第一次选择后,我必须将该列更改为trueUPDATE user_message 设置为\u read=true,其中username=truejohn777@gmail.com';表示用户已阅读消息。所以问题是我可以做一个查询并通过批处理执行它,还是应该

我有
query=“select*from user\u message,其中username=”john777@gmail.com“;”
。有一列“is_read”(当消息发送给用户时,is_read默认为false,这意味着用户尚未阅读消息),在第一次选择后,我必须将该列更改为true
UPDATE user_message

设置为\u read=true,其中username=truejohn777@gmail.com';表示用户已阅读消息。所以问题是我可以做一个查询并通过批处理执行它,还是应该做两个不同的查询?哪种方法更好?

您正在编写一个带有两个参数的方法username,并且正在读取。您可以在需要时随时调用此方法。

我认为您的update语句不正确,因为它没有传递消息id

我猜是这样的:

UPDATE user_message
SET is_read=true 
where username = 'john777@gmail.com'
and user_message_id = 123
如果要将多条消息设置为
读取
,可以通过以下方式执行此操作:

UPDATE user_message
SET is_read=true 
where username = 'john777@gmail.com'
and user_message_id in (123, 234, 456)

我觉得这是一个设计缺陷。为什么user_message表中有电子邮件?如果用户更改了电子邮件,会发生什么情况?您应该为每个用户提供一个唯一的(整数)id作为外键,从而真正将电子邮件与用户分离。

基于假设您有一个
参数集合,比如
电子邮件
,我建议如下:

String sql = "Your statement";
PreparedStatement stmt = conection.prepare(sql);
for(String email : emails){
   stmt.setString(1, email);
   stmt.executeUpdate();
   stmt.clearParameters();
}

我认为这将对您有所帮助。

您可以通过每次只设置参数,使用单个preparedStatement循环更新要更新的值。