PreparedStatement批量处理的一个Framework(原创)

简介: 场景:批量进行DML操作,但涉及的表不同,不能使用executeBatch()需求:(1)如果DML中有一个错误时,要全部回滚;(2)如果全部正确,要全部执行;解决方案: package jdbc; import java.

场景:
批量进行DML操作,但涉及的表不同,不能使用executeBatch()

需求:
(1)如果DML中有一个错误时,要全部回滚;
(2)如果全部正确,要全部执行;

解决方案:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class DuplicateStatmentProcessor {
    private String dirver;
    private String url;
    private String user;
    private String password;

    public DuplicateStatmentProcessor(String dirver, String url, String user, String password) {
        super();
        this.dirver = dirver;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public void process(Map<String, List<String>> sqlWithParams) throws ClassNotFoundException, SQLException {
        Class.forName(dirver);
        Connection conn = null;
        PreparedStatement psmt = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            conn.setAutoCommit(false);

            Iterator<Entry<String, List<String>>> iterator = sqlWithParams.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, List<String>> entry = iterator.next();
                psmt = conn.prepareStatement(entry.getKey());
                int parameterIndex = 1;
                for (String parameter : entry.getValue()) {
                    psmt.setString(parameterIndex, parameter);
                    parameterIndex++;
                }
                psmt.executeUpdate();
            }
            conn.commit();
        } catch (SQLException e) {
            // 如果出错,则此次executeBatch()的所有数据都不入库
            conn.rollback();
            e.printStackTrace();
        } finally {
            conn.setAutoCommit(true);
            close(conn, psmt);
        }

    }

    private void close(Connection conn, PreparedStatement preStmt) {
        if (preStmt != null) {
            try {
                preStmt.clearBatch();
                preStmt.clearParameters();
                preStmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.setAutoCommit(true);
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

 

package jdbc.oracle.demo1;

import jdbc.DuplicateStatmentProcessor;

public class OracleProcessor extends DuplicateStatmentProcessor {

    public OracleProcessor() {
        // oracle.jdbc.driver.OracleDriver
        super("oracle.jdbc.OracleDriver",
                "jdbc:oracle:thin:@127.0.0.1:1521:instance1",
                "user",
                "password");
    }

}

 

package jdbc.oracle.demo1;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jdbc.DuplicateStatmentProcessor;

/*2015-8-4*/
public class Processor {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String DEL_TB_PERSON_SQL = "delete from TB_PERSON where id=?";
        String DEL_TB_COURSE_SQL = "delete from tb_course where id=?";

        Map<String, List<String>> sqlWithParams = new HashMap<String, List<String>>();
        sqlWithParams.put(DEL_TB_PERSON_SQL, Arrays.asList("1"));
        sqlWithParams.put(DEL_TB_COURSE_SQL, Arrays.asList("3"));

        DuplicateStatmentProcessor processor = new OracleProcessor();
        processor.process(sqlWithParams);
    }

}


结果:
测试通过


Tips:
关于executeBatch参见http://www.cnblogs.com/softidea/p/4663090.html

相关文章
|
Java
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
1337 2
|
自然语言处理 算法 数据挖掘
自蒸馏:一种简单高效的优化方式
背景知识蒸馏(knowledge distillation)指的是将预训练好的教师模型的知识通过蒸馏的方式迁移至学生模型,一般来说,教师模型会比学生模型网络容量更大,模型结构更复杂。对于学生而言,主要增益信息来自于更强的模型产出的带有更多可信信息的soft_label。例如下右图中,两个“2”对应的hard_label都是一样的,即0-9分类中,仅“2”类别对应概率为1.0,而soft_label
自蒸馏:一种简单高效的优化方式
|
开发工具 Nacos git
Git如何checkout远程tag
Git如何checkout远程tag
4024 0
|
开发工具 git
git基于tag创建分支
git基于tag创建分支
|
10月前
|
消息中间件 运维 数据库
Seata框架和其他分布式事务框架有什么区别
Seata框架和其他分布式事务框架有什么区别
194 1
|
10月前
|
数据采集 人工智能 自然语言处理
关于大模型语料的迷思
随着大模型发展的不断深入,我们越来越关注到语料质量对模型能力的影响,语料中的偏差和主观性会导致生成内容不准确或带有偏见。智能引擎事业部是阿里内部深耕多年的AI工程团队,为内部业务提供了完整的大模型工程体系,持续关注大模型训推性能、成本、研发范式等关键问题。本文将基于我们的思考,探讨大模型语料的复杂性及其背后的思维过程。
|
存储 缓存 NoSQL
数据缓存,可以尝试用RocksDB了
`shigen`,一个专注于Java、Python、Vue和Shell的博主,探讨了为何在学习阿里云DRM产品时选择RocksDB而非Redis或Guava。RocksDB是一个高速、可配置的存储系统,适用于Flash和HDFS,支持数据压缩。与Redis相比,RocksDB在高速存储和灵活性上更具优势。在尝试使用RocksDB与SpringBoot集成时遇到问题,目前尚未解决。他还对比了RocksDB、Redis和Guava Cache的特性,强调RocksDB适合大规模、高性能场景,而Redis适合内存存储和实时性需求。
306 0
数据缓存,可以尝试用RocksDB了
|
算法 关系型数据库 MySQL
Mysql为何建议使用自增id作主键,有什么优点
Mysql为何建议使用自增id作主键,有什么优点
1581 1
|
消息中间件 存储 算法
MQ - 闲聊MQ一二事儿 (Kafka、RocketMQ 、Pulsar )
MQ - 闲聊MQ一二事儿 (Kafka、RocketMQ 、Pulsar )
721 0