微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 准备好的声明,批量更新,不同的更新

是否可以进行批量更新但是有不同的声明?我找到的每个教程和在线资源都使用相同的语句.这就是我所拥有的,但它不起作用.

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) {}
    }
}

解决方法:

您不能在一个批处理中执行多个不同的语句,但是,您可以使用一个存储过程,可以在单个往返服务器中完成所有操作,同时保持单个事务的好处

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐