Bai 2

package cuoiki; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; /** * * @author Tonqt Thonq */ public class Bai2 { public static void main(String[] args) throws FileNotFoundException, IOException { String fn = "D://input.txt"; ArrayList<String> data = new ArrayList<String>(); int count = 1; try{ File f = new File(fn); FileInputStream fis = new FileInputStream(f); BufferedReader bf = new BufferedReader(new InputStreamReader(fis)); String temp; while((temp = bf.readLine())!=null) { data.add(temp); } } catch(Exception emsg){ }; ArrayList<HS> list_hs= new ArrayList<HS>(); for (String s : data){ char arr[] = s.toCharArray(); String MS="", HT="", date=""; for(int i=0; i<=2; i++) MS+=arr[i]; for(int i=3; i<=12; i++) HT+=arr[i]; for(int i=13; i<arr.length; i++) date+=arr[i]; MS=MS.replaceAll(" ", ""); HT=HT.replaceAll("\\s+",""); date=date.replaceAll(" ",""); SimpleDateFormat d1 = new SimpleDateFormat("dd/mm/yyyy"); SimpleDateFormat d2 = new SimpleDateFormat("yyyy-mm-dd"); try{ Date d = d1.parse(date); date = d2.format(d); HS h = new HS(MS, HT, date); list_hs.add(h); System.out.println(""+h.toString()); } catch(ParseException e ) { File f = new File("D://log.txt"); if(!f.exists()){ f.createNewFile();} FileWriter fw = new FileWriter(f,true); BufferedWriter bf = new BufferedWriter(fw); bf.write("Dong "+count+" loi đinh dang ngay\n"); bf.close(); fw.close(); }; count++; } try{ Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password="); if(con!=null) { System.out.println("Connected"); Statement stt = con.createStatement(); for(HS h : list_hs){ String sql = "INSERT INTO bai2(MS,HT,ngay) VALUES("+h.toString()+")"; stt.executeUpdate(sql); } }; } catch (Exception e){ System.err.println(""+e.toString()); } } } class HS{ String MS, HT, date; public HS(String MS, String HT, String Date) { this.MS = MS; this.HT = HT; this.date = Date; } @Override public String toString() { return String.valueOf("'"+MS+"','"+HT+"','"+date+"'"); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.