2015-03-31 60 views
-1
package com.test.mysql; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 
import com.test.mysql.FileReaderer; 
public class automateImport { 


    /** 
    * @param args 
    * @throws ClassNotFoundException 
    */ 
    public static void main(String[] args) throws ClassNotFoundException, FileNotFoundException { 


     /* Class.forName("com.mysql.jdbc.Driver"); 
     try { 
      Connection con = (Connection) DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/mysql", "root", "root"); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }*/ 
     FileReaderer fr = null; 
     String dirpath = ""; 
     Scanner scanner1 = new Scanner(System.in); 

     while (true) { 
      System.out.println("Please give the directory:"); 
      dirpath = scanner1.nextLine(); 
      File fl = new File(dirpath); 
      System.out.println("f1"+fl.getPath()); 
      if (fl.canRead()){ 
       System.out.println("f2"+fl.getPath()); 
       fr = new FileReaderer(fl); 
       break; 
      } 
      else{ 
       System.out.println("Error:Directory does not exists"); 
      } 
     } 
    } 
} 
package com.test.mysql; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import javax.swing.filechooser.FileFilter; 
import javax.swing.filechooser.FileNameExtensionFilter; 


    public class FileReaderer { 

     private final Pattern linePattern = Pattern.compile("^(\\w++)\\s++(\\w++)\\s*+$"); 
     private final Pattern lineBreakPattern = Pattern.compile("\r?\n"); 
     private final FileFilter txtFilter = new FileNameExtensionFilter("*.txt", "txt"); 
     private final File txtFolder; 
     Connection con = null; 
     Statement stmt = null; 

     public FileReaderer(File txtFolder) { 
      this.txtFolder = txtFolder; 
      readFiles(); 
     } 

     public List<Person> readFiles() { 

      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root"); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (ClassNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      final List<Person> people = new LinkedList<>(); 
      System.out.println("txt folder"+txtFolder.getPath()); 
      for (final File txtFile : txtFolder.listFiles()) { 
       if (txtFilter.accept(txtFile)) { 
        System.out.println("txt Files " +txtFile.getName()); 
        people.addAll(readFile(txtFile)); 

       } 
      } 
      System.out.println("File Final List==>"+people); 

       insertData(con,stmt,people);  

      return people; 
     } 

     private List<Person> readFile(File txtFile) { 
      try (final Scanner scanner = new Scanner(txtFile)) { 
       /* scanner.useDelimiter(lineBreakPattern); 
       final Person person = new Person(); 
       while (scanner.hasNext()) { 
        final String line = scanner.next(); 
        final Matcher matcher = linePattern.matcher(line); 
        if (matcher.matches()) { 
         switch (matcher.group(1).toUpperCase()) { 
          case "ID": 
           person.setId(Integer.parseInt(matcher.group(2))); 
           break; 
          case "NAME": 
           person.setName(matcher.group(2)); 
           break; 
          default: 
           throw new IOException("Illegal line '" + matcher.group() + "'."); 
         } 
        } 
       }*/ 
       BufferedReader br = new BufferedReader(new FileReader(txtFile)); 

       String currentLine = br.readLine(); 

       List<Person> processPersonList = new ArrayList<Person>(); 
       while (currentLine != null) { 

        String[] tokens = currentLine.split(","); 
        Person finalPerson = new Person(); 
        finalPerson.setFirstName(tokens[0]); 
        finalPerson.setLastName(tokens[1]); 
        finalPerson.setSIN(tokens[2]);     
        currentLine = br.readLine(); 
        processPersonList.add(finalPerson); 

       } 
       System.out.println("final list==>"+processPersonList); 

       br.close(); 
       return processPersonList; 
      } catch (IOException ex) { 
       throw new RuntimeException(ex); 
      } 
     } 

     private void insertData(Connection con,Statement stmt,List<Person> pp){ 

      System.out.print("\nInserting records into table..."); 
      try{ 
       for(Person pr:pp){ 
        System.out.println("First Name " +pr.getFirstName()+" Second Name " +pr.getLastName()+"SIN Name " +pr.getSIN()); 

      String sql = "INSERT INTO employee(first_name, last_name,sin) values (?,?,?)" ; 
      PreparedStatement preparedStmt = con.prepareStatement(sql); 
      preparedStmt.setString (1, pr.getFirstName()); 
      preparedStmt.setString (2, pr.getLastName()); 
      preparedStmt.setString (3, pr.getSIN()); 
      preparedStmt.execute(); 
       } 

      System.out.println(" SUCCESS!\n"); 
      con.close(); 
      } catch (Exception e) 
      { 
       System.err.println("Got an exception!"); 
       System.err.println(e.getMessage()); 
      } 
     } 
    } 

package com.test.mysql; 

public class Person { 



     private String firstName; 
     private String lastName; 
     private String SIN; 

     public String getFirstName() { 
      return firstName; 
     } 
     public void setFirstName(String firstName) { 
      this.firstName = firstName; 
     } 
     public String getLastName() { 
      return lastName; 
     } 
     public void setLastName(String lastName) { 
      this.lastName = lastName; 
     } 
     public String getSIN() { 
      return SIN; 
     } 
     public void setSIN(String sIN) { 
      SIN = sIN; 
     } 


// 

連接越來越關閉之前插入到數據庫&需要實現排序 連接插入到數據庫&之前得到閉合需要實現排序 連接之前得到封閉插入到數據庫&需要實行分類 連接插入到數據庫&之前得到閉合需要實現排序轉儲CSV文件導入到數據庫造成的錯誤連接關閉

} 
+0

什麼是工作,什麼不是?我在你的文章中找不到一個英文句子。 – luk2302 2015-03-31 18:15:20

回答

-1
 package com.test.readfile; 

     import java.util.Date; 

     public class Employee { 

      private long id; 
      private String firstName; 
      private String lastName; 
      private Double salary; 
      private String sin; 
      private Date dob; 


      public String getFirstName() { 
       return firstName; 
      } 
      public void setFirstName(String firstName) { 
       this.firstName = firstName; 
      } 
      public String getLastName() { 
       return lastName; 
      } 
      public void setLastName(String lastName) { 
       this.lastName = lastName; 
      } 
      public Double getSalary() { 
       return salary; 
      } 
      public void setSalary(Double salary) { 
       this.salary = salary; 
      } 
      public String getSin() { 
       return sin; 
      } 
      public void setSin(String sin) { 
       this.sin = sin; 
      } 
      public long getId() { 
       return id; 
      } 
      public void setId(long id) { 
       this.id = id; 
      } 
      public Date getDob() { 
       return dob; 
      } 
      public void setDob(Date dob) { 
       this.dob = dob; 
      } 



     } 

     package com.test.readfile; 

     import java.io.BufferedReader; 
     import java.io.File; 
     import java.io.FileReader; 
     import java.io.IOException; 
     import java.sql.Connection; 
     import java.sql.Date; 
     import java.sql.DriverManager; 
     import java.sql.PreparedStatement; 
     import java.sql.SQLException; 
     import java.sql.Statement; 
     import java.sql.Timestamp; 
     import java.text.DateFormat; 
     import java.text.ParseException; 
     import java.text.SimpleDateFormat; 
     import java.util.ArrayList; 
     import java.util.HashMap; 
     import java.util.LinkedList; 
     import java.util.List; 
     import java.util.Map; 
     import java.util.Scanner; 
     import java.util.regex.Matcher; 
     import java.util.regex.Pattern; 

     import javax.swing.filechooser.FileFilter; 
     import javax.swing.filechooser.FileNameExtensionFilter; 


      public class FileReaderer { 

       private final Pattern linePattern = Pattern.compile("^(\\w++)\\s++(\\w++)\\s*+$"); 
       private final Pattern lineBreakPattern = Pattern.compile("\r?\n"); 
       private final FileFilter txtFilter = new FileNameExtensionFilter("*.txt", "txt"); 
       private final File txtFolder; 
       Connection con = null; 
       Statement stmt = null; 

       public FileReaderer(File txtFolder) { 
        this.txtFolder = txtFolder; 
        readFiles(); 
       } 

       public List<Employee> readFiles() { 

        try { 
         Class.forName("com.mysql.jdbc.Driver"); 
         con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "ptlusrapp$246"); 
        } catch (SQLException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        catch (ClassNotFoundException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
        final List<Employee> people = new LinkedList<>(); 
        System.out.println("txt folder"+txtFolder.getPath()); 
        for (final File txtFile : txtFolder.listFiles()) { 
         if (txtFilter.accept(txtFile)) { 
          System.out.println("txt Files " +txtFile.getName()); 
          people.addAll(readFile(txtFile)); 

         } 
        } 
        System.out.println("File Final List==>"+people); 

         insertData(con,stmt,people);  

        return people; 
       } 

       private List<Employee> readFile(File txtFile) { 
        try (final Scanner scanner = new Scanner(txtFile)) { 
         /* scanner.useDelimiter(lineBreakPattern); 
         final Employee Employee = new Employee(); 
         while (scanner.hasNext()) { 
          final String line = scanner.next(); 
          final Matcher matcher = linePattern.matcher(line); 
          if (matcher.matches()) { 
           switch (matcher.group(1).toUpperCase()) { 
            case "ID": 
             Employee.setId(Integer.parseInt(matcher.group(2))); 
             break; 
            case "NAME": 
             Employee.setName(matcher.group(2)); 
             break; 
            default: 
             throw new IOException("Illegal line '" + matcher.group() + "'."); 
           } 
          } 
         }*/ 
         BufferedReader br = new BufferedReader(new FileReader(txtFile)); 

         String currentLine = br.readLine(); 
         DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); 
         java.util.Date convertedDate = null; 

         String splitterString="\\s"; 

         List<Employee> processEmployeeList = new ArrayList<Employee>(); 
         while (currentLine != null) { 

          if(currentLine.indexOf(",")>-1) 
           splitterString = ","; 
          else 
           splitterString = "\\|"; 

          String[] tokens = currentLine.split(splitterString); 
          Employee finalEmployee = new Employee(); 
          finalEmployee.setId(Long.parseLong(tokens[0])); 
          finalEmployee.setFirstName(tokens[1]); 
          finalEmployee.setLastName(tokens[2]);     
          finalEmployee.setSalary(Double.parseDouble(tokens[3])); 
          finalEmployee.setSin(tokens[4]); 
          try { 
          convertedDate = formatter.parse(tokens[5]); 
          finalEmployee.setDob(convertedDate); 
         } catch (ParseException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

         // finalEmployee.setDob(convertedDate); 
          currentLine = br.readLine(); 
          processEmployeeList.add(finalEmployee); 

         } 
         System.out.println("final list==>"+processEmployeeList); 

         br.close(); 
         return processEmployeeList; 
        } catch (IOException ex) { 
         throw new RuntimeException(ex); 
        } 
       } 

       private void insertData(Connection con,Statement stmt,List<Employee> pp){ 

        System.out.print("\nInserting records into table..."); 
        try{ 
         for(Employee pr:pp){ 
          System.out.println("First Name " +pr.getFirstName()+" Second Name " +pr.getLastName()+"SIN Name " +pr.getSin() +"DOB: "+pr.getDob()); 

        //String sql = "INSERT INTO employee(first_name, last_name,sin) values (?,?,?)" ; 
        String sql = "INSERT INTO employee(id,first_name,last_name,salary,sin,dob) values (?,?,?,?,?,?)" ; 
        PreparedStatement preparedStmt = con.prepareStatement(sql); 
        preparedStmt.setLong(1,pr.getId()); 
        preparedStmt.setString (2, pr.getFirstName()); 
        preparedStmt.setString (3, pr.getLastName()); 
        preparedStmt.setDouble(4, pr.getSalary()); 
        preparedStmt.setString (5, pr.getSin()); 
        java.sql.Date sqlDate = new java.sql.Date(pr.getDob().getTime()); 
        preparedStmt.setDate(6, sqlDate); 

        preparedStmt.execute(); 
         } 

        System.out.println(" SUCCESS!\n"); 
        con.close(); 
        } catch (Exception e) 
        { 
         System.err.println("Got an exception!"); 
         System.err.println(e.getMessage()); 
        } 
       } 
      } 


     package com.test.readfile; 
     import java.io.BufferedReader; 
     import java.io.File; 
     import java.io.FileNotFoundException; 
     import java.io.FileReader; 
     import java.sql.Connection; 
     import java.sql.DriverManager; 
     import java.sql.Statement; 
     import java.sql.ResultSet; 
     import java.sql.SQLException; 
     import java.util.ArrayList; 
     import java.util.List; 
     import java.util.Scanner; 

     public class AutomateImport { 

      /** 
      * @param args 
      * @throws ClassNotFoundException 
      */ 
      public static void main(String[] args) throws ClassNotFoundException, FileNotFoundException { 


       /* Class.forName("com.mysql.jdbc.Driver"); 
       try { 
        Connection con = (Connection) DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/mysql", "root", "root"); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }*/ 
       FileReaderer fr = null; 
       String dirpath = ""; 
       Scanner scanner1 = new Scanner(System.in); 

       while (true) { 
        System.out.println("Please give the directory:"); 
        dirpath = scanner1.nextLine(); 
        File fl = new File(dirpath); 
        System.out.println("f1"+fl.getPath()); 
        if (fl.canRead()){ 
         System.out.println("f2"+fl.getPath()); 
         fr = new FileReaderer(fl); 
         break; 
        } 
        else{ 
         System.out.println("Error:Directory does not exists"); 
        } 
       } 
      } 
     }