json代碼駕照考題批量加入MySQL資料庫 ps.executeUpdate()永遠只能悲催的加一條資料 去掉id主鍵自增 for 與 foreach

來源:互聯網
上載者:User

標籤:gson   code   fileinput   finally   bsp   null   url   set   else   

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的方法,查詢可以修改不行                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();//執行批處理            return true;        } catch (SQLException e) {            e.printStackTrace();        } finally {            JDBCUtil.closeAll(conn, ps, null);        }        return false;    }}

 

json代碼駕照考題批量加入MySQL資料庫 ps.executeUpdate()永遠只能悲催的加一條資料 去掉id主鍵自增 for 與 foreach

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.