PackageCom.swift;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.io.UnsupportedEncodingException;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importjava.util.List;ImportCom.google.gson.Gson; Public classAddAll {/*** The JSON driver test code in the Jzkt.json file under the D-drive D:\java_JDK\java_date path is parsed into the root object using Gson*/ Public Static voidMain (string[] args) {File file=NewFile ("D:\\java_jdk\\java_date", "Jzkt.json"); StringBuilder SB=NULL; List<Result> list =NULL; Try{bufferedreader br=NewBufferedReader (NewInputStreamReader (NewFileInputStream (file), "Utf-8")); String Str=NULL; SB=NewStringBuilder (); FinalString line_separator = System.getproperty ("Line.separator"); Try { while(str = br.readline ())! =NULL) {sb.append (str); Sb.append (Line_separator); } Gson Gson=NewGson (); Root root= Gson.fromjson (Sb.tostring (), Root.class); List= Root.getresult ();//gets the object list collection for the result type}Catch(IOException e) {e.printstacktrace (); } } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); } if(AddAll (list)) {System.out.println ("Add Success"); } Else{System.out.println ("Add Failed"); } } Private Static BooleanAddAll (list<result>list) {Connection conn=Jdbcutil.getconn (); PreparedStatement PS=NULL; Try{PS= Conn.preparestatement ("INSERT into sw_question values (?,?,?,?,?,?,?,?,?)"); for(inti = 0; I < list.size (); i++) {//do not use the Foreach method, the query can be modified noSystem.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 ();//add to batch, use Ps.executeupdate () always only sad to add a piece of data} ps.executebatch ();//perform batch processing return true; } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.closeall (conn, PS,NULL); } return false; }}
JSON code driver's License questions batch join MySQL database ps.executeupdate () always only sad to add a piece of data to remove the ID primary key auto-increment for and foreach