json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 for 与 foreach
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 for 与 foreach,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2583字,纯文字阅读大概需要4分钟。
内容图文
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
标签:gson code fileinput finally bsp null url set else
本文系统来源:http://www.cnblogs.com/qingyundian/p/7608305.html
内容总结
以上是互联网集市为您收集整理的json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 for 与 foreach全部内容,希望文章能够帮你解决json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 for 与 foreach所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。