java jdbc addBatch()提交mysql速度过慢

查看:327
本文介绍了java jdbc addBatch()提交mysql速度过慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我用的是java jdbc的addBatch批量插入数据,但是速度好慢有什么好的方案吗
差不多插入1000条数据需要30s,下面贴出代码大家看看哪里不规范,或者给个更快的方法

//jdbc构造的URL是
private static final String URL="jdbc:mysql://localhost:3306/daan?useUnicode=true&characterEncoding=UTF8&rewriteBatchedStatements=true";

public static void writeData(LinkedList<Entity> entities)
    {
        con = DBUtil.getCon();
        String sql = "INSERT INTO `test` (`title`,`url`,`year`,`grade`,`subject`,`publish`) VALUES (?,?,?,?,?,?)";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            for (int i = 0;i<entities.size();i++)
            {
                Entity t = entities.get(i);
                preparedStatement.setString(1,t.getTitle());
                preparedStatement.setString(2,t.getUrl());
                preparedStatement.setInt(3,t.getYear());
                preparedStatement.setInt(4,t.getGrade());
                preparedStatement.setInt(5,t.getSubject());
                preparedStatement.setString(6,t.getPublish());
                preparedStatement.addBatch();
                //System.out.println(preparedStatement.toString());
                preparedStatement.execute();
            }
        } catch (SQLException e) {
            System.out.println("数据库错误"+e.getMessage());
        }
    }

解决方案

JDBC BATCH INSERT.

这篇关于java jdbc addBatch()提交mysql速度过慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆