java – 准备好的声明,批量更新,不同的更新
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 准备好的声明,批量更新,不同的更新,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2587字,纯文字阅读大概需要4分钟。
内容图文
![java – 准备好的声明,批量更新,不同的更新](/upload/InfoBanner/zyjiaocheng/770/6f949abfe1344df890db526cbddc0783.jpg)
是否可以进行批量更新但是有不同的声明?我找到的每个教程和在线资源都使用相同的语句.这就是我所拥有的,但它不起作用.
public static void resetNotifications(User user)
{
DataSource dataSource = null;
Context initialContext = null;
Context environmentContext = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
String stride = "UPDATE stride SET recipientView = 1 WHERE recipientId = ?";
String strideLike = "UPDATE strideLike SET recipientView = 1 WHERE recipientId = ? ";
String strideCommentMe = "UPDATE strideComment SET recipientView = 1 WHERE recipientId = ? ";
String strideCommentLike = "UPDATE strideCommentLike SET recipientView = 1 WHERE recipientId = ? ";
String strideCommentFollow = "UPDATE strideCommentNotification SET recipientView = 1 WHERE userId = ? ";
try
{
initialContext = new InitialContext();
environmentContext = (Context) initialContext.lookup("java:/comp/env");
dataSource = (DataSource) environmentContext.lookup("jdbc/instride");
connection = dataSource.getConnection();
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(stride);
preparedStatement.setLong(1, user.getId());
preparedStatement.addBatch();
preparedStatement = connection.prepareStatement(strideLike);
preparedStatement.setLong(1, user.getId());
preparedStatement.addBatch();
preparedStatement = connection.prepareStatement(strideCommentMe);
preparedStatement.setLong(1, user.getId());
preparedStatement.addBatch();
preparedStatement = connection.prepareStatement(strideCommentLike);
preparedStatement.setLong(1, user.getId());
preparedStatement.addBatch();
preparedStatement = connection.prepareStatement(strideCommentFollow);
preparedStatement.setLong(1, user.getId());
preparedStatement.addBatch();
preparedStatement.executeBatch();
connection.commit();
}
catch(NamingException error)
{
System.out.println("NotificationsDAO - resetNotifications() - Error With JNDI Lookup - " + error.getMessage());
error.printStackTrace();
}
catch(SQLException error)
{
System.out.println("NotificationsDAO - resetNotifications() - Error With SQL - " + error.getMessage());
error.printStackTrace();
}
finally
{
if (initialContext != null) try{initialContext.close();} catch(NamingException ignore) {}
if (environmentContext != null) try{environmentContext.close();} catch(NamingException ignore) {}
if (connection != null) try{connection.close();} catch(SQLException ignore) {}
if (preparedStatement != null) try{preparedStatement.close();} catch(SQLException ignore) {}
}
}
解决方法:
您不能在一个批处理中执行多个不同的语句,但是,您可以使用一个存储过程,可以在单个往返服务器中完成所有操作,同时保持单个事务的好处
内容总结
以上是互联网集市为您收集整理的java – 准备好的声明,批量更新,不同的更新全部内容,希望文章能够帮你解决java – 准备好的声明,批量更新,不同的更新所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。