json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句

package com.swift;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.google.gson.Gson;

public class AddAll {
    /**
     * 将D盘D:\java_JDK\java_date路径下的jzkt.json文件中的json驾照考试代码用Gson解析成root对象
     */
    public static void main(String[] args) {
        File file = new File("D:\\java_JDK\\java_date", "jzkt.json");
        StringBuilder sb = null;
        List<Result> list = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            String str = null;
            sb = new StringBuilder();
            final String LINE_SEPARATOR = System.getProperty("line.separator");
            try {
                while ((str = br.readLine()) != null) {
                    sb.append(str);
                    sb.append(LINE_SEPARATOR);
                }
                Gson gson = new Gson();
                Root root = gson.fromJson(sb.toString(), Root.class);
                list = root.getResult();//得到Result类型的对象列表集合
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (addAll(list)) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private static boolean addAll(List<Result> list) {

        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        try {

            ps = conn.prepareStatement("insert into sw_question values(?,?,?,?,?,?,?,?,?)");
            for (int i = 0; i < list.size(); i++) {//不要用foreach的方法,查询可以修改不行,用foreach循环数据库只出现一条语句
                System.out.println("i=" + i);
                ps.setInt(1, list.get(i).getId());
                ps.setString(2, list.get(i).getQuestion());
                ps.setString(3, list.get(i).getAnswer());
                ps.setString(4, list.get(i).getItem1());
                ps.setString(5, list.get(i).getItem2());
                ps.setString(6, list.get(i).getItem3());
                ps.setString(7, list.get(i).getItem4());
                ps.setString(8, list.get(i).getExpalins());
                ps.setString(9, list.get(i).getUrl());
                ps.addBatch();//添加到批处理 ,使用ps.executeUpdate()永远只能悲催的加一条数据
                
            }
            ps.executeBatch();//执行批处理,如果使用foreach,这句要放到循环当中,但影响程序执行速度
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(conn, ps, null);
        }
        return false;
    }
}

 

posted @ 2017-09-28 20:14  Advancing-Swift  阅读(559)  评论(0编辑  收藏  举报