[英]preparedstatement batch fo insert and update?
我有query = "select * from user_message where username = 'john777@gmail.com';"
。 有一列'is_read'(當消息發送給用戶時,is_read默認為false,這意味着用戶尚未讀取消息),在第一次選擇后,我必須將該列更改為true UPDATE user_message SET is_read=true where username = 'john777@gmail.com';
這表示用戶已閱讀消息。 所以問題是我可以進行一個查詢並通過批處理執行它還是應該進行兩個不同的查詢? 哪種方法更好?
您正在編寫一個帶有兩個參數username和is_read的方法。 您可以在需要時隨時調用此方法。
我認為您的更新語句不正確,因為它沒有傳遞消息ID
我猜是這樣的:
UPDATE user_message
SET is_read=true
where username = 'john777@gmail.com'
and user_message_id = 123
如果您想設置多條消息來read
,可以通過以下方式進行操作:
UPDATE user_message
SET is_read=true
where username = 'john777@gmail.com'
and user_message_id in (123, 234, 456)
對我來說,這感覺像是設計缺陷。 為什么user_message表包含電子郵件? 如果用戶更改電子郵件,會發生什么? 您應該通過給每個用戶一個唯一的(整數)ID(用作外鍵)來使電子郵件與用戶脫鈎。
基於假設您有一個參數Collection
,例如emails
,我建議以下內容:
String sql = "Your statement";
PreparedStatement stmt = conection.prepare(sql);
for(String email : emails){
stmt.setString(1, email);
stmt.executeUpdate();
stmt.clearParameters();
}
我認為這會對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.