2015-09-09 54 views
0

我有query = "select * from user_message where username = '[email protected]';"。有一列'is_read'(当消息发送给用户,is_read默认为false,这意味着用户还没有阅读消息),第一次选择后,我必须将该列更改为真UPDATE user_message SET is_read=true where username = '[email protected]';这意味着用户已阅读消息。所以问题是我可以做一个查询,并通过批处理执行它,或者我应该做两个不同的查询?哪种方法更好?preparedstatement批次插入和更新?

+0

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

回答

0

您正在编写一个带有两个参数username和is_read的方法。你可以在需要时随时调用这个方法。

0

我觉得你的更新语句不正确,因为它没有通过的消息ID

我猜它是这样的:

UPDATE user_message 
SET is_read=true 
where username = '[email protected]' 
and user_message_id = 123 

如果你想多封邮件设置为read你可以这样做:

UPDATE user_message 
SET is_read=true 
where username = '[email protected]' 
and user_message_id in (123, 234, 456) 

这感觉就像一个设计缺陷给我。为什么user_message表有电子邮件?如果用户更改电子邮件会发生什么情况?你应该通过给每个用户一个唯一的(整数)ID作为外键来解耦来自用户的电子邮件。

0

基于这样的假设在你的参数Collectionemails,我建议如下:

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

这个我想会帮助你。