JDBC批处理实践

package com.wendao.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
public class Test3 {

    public static void main(String[] args) {
        String s = "101,102,102,103,103,103,104,104,104,104,105,106,106,106,106,107,108,109,110";

        // String转list
        List<String> Z = Arrays.asList(s.split(","));
        //Z :  [101, 102, 102, 103, 103, 103, 104, 104, 104, 104, 105, 106, 106, 106, 106, 107, 108, 109, 110]
        System.out.println("String转list:  " + Z);
        
        //List之统计元素项以及元素出现的次数
        List<Map<String, Object>> collectionsZqt = CollectionsList(Z);
        //collectionsZqt  :  [{th=110, count=1}, {th=101, count=1}, {th=102, count=2}, {th=103, count=3}, {th=104, count=4}, {th=105, count=1}, {th=106, count=4}, {th=107, count=1}, {th=108, count=1}, {th=109, count=1}]
        System.out.println("List之统计元素项以及元素出现的次数:  "+collectionsZqt.toString());
        
        //List转JSONArray
        JSONArray jsonArray = new JSONArray(collectionsZqt);
        //jsonArray : [{"th":110,"count":1},{"th":101,"count":1},{"th":102,"count":2},{"th":103,"count":3},{"th":104,"count":4},{"th":105,"count":1},{"th":106,"count":4},{"th":107,"count":1},{"th":108,"count":1},{"th":109,"count":1}]
        System.out.println("List转JSONArray:  "+jsonArray);

        String sql = "UPDATE`tb_yct` SET ZQCS = ZQCS+? WHERE TBH = ?";
    }

    private void Plgx(JSONArray jsonArray, String sql) throws Exception {

        
        String url = "jdbc:mysql://59.110.237.95:3306/xxbd?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior\\=convertToNull" + "&rewriteBatchedStatements=true";
        String user = "xxx";
        String pwd = "xxx";

        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //数据库连接
            conn = DriverManager.getConnection(url, user, pwd);
            // conn = JDBCUtils.getConnection();  也可以使用封装的工具类连接
            // String sql = "UPDATE`tb_yct` SET ZQCS = ZQCS+? WHERE TBH = ?";   这是需要批处理的sql语句
            pstmt = conn.prepareStatement(sql);
            for (int i = 1; i < jsonArray.length(); i++) {
                JSONObject object = (JSONObject) jsonArray.get(i);
                Integer int1 = (Integer) object.get("th");
                Integer int2 = (Integer) object.get("count");
                //放入参数,这"1"、"2"是第几个参数的意思
                pstmt.setInt(1, int2);
                pstmt.setInt(2, int1);
                pstmt.addBatch();
                pstmt.executeBatch();
                if (i % 1000 == 0) {
                    //清理
                    pstmt.clearBatch();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            //工具类的释放方法
            // JDBCUtils.release(null, pstmt, conn);
        }
    }
    
    //静态方法
    //List之统计元素项以及元素出现的次数
        private static List<Map<String, Object>> CollectionsList(List<String> Z) {
            //new一个list准备装取统计后的数据
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
            //转换成set去重
            Set<String> set = new HashSet<String>(Z);  
            //for循环统计set中元素出现的次数
            for(String id : set){
                //new一个map来装取统计后的元素次数
                Map<String,Object> map = new HashMap<String, Object>();  
                //转换成int类型
                int ids = Integer.parseInt(id);
                map.put("th",ids);  
                map.put("count", Collections.frequency(Z, id));  
                list.add(map);  
            }  
//            System.out.println(list.toString());  
            return list;
        }
}

猜你喜欢

转载自blog.csdn.net/luker_/article/details/80632134
今日推荐