2016-11-06 41 views
1

我需要一些我正在處理的項目的幫助。該項目的第一部分是創建一個生成假數據並將其寫入CSV文本文件的實用程序/工具。這部分工作得很好。在Java和MySQL數據庫中讀取文件

下面是該代碼(如果有幫助)

import sys 
from faker import Factory 
fake = Factory.create() 

x = 1 
param1 = int(sys.argv[1]) 
f = open('myfile.txt','w') 
for x in range (0,param1): 
f.write(fake.first_name() + "," + fake.last_name() + "," + fake.job() + "," + fake.email() + "," 
    + fake.street_address() + "," + fake.city() + "," + fake.state() + "," + fake.state_abbr() + "," 
    + fake.zipcode() + "," + fake.credit_card_provider() + "," 
    + fake.credit_card_number() + "," + fake.phone_number() + "\n") 

f.close() 

下面是編譯時什麼打印出來:每行之間

William,James,Careers information officer,[email protected],9448 Rodriguez Brook Apt. 796,South Lynnbury,South Carolina,VA,26103,JCB 16 digit,3112583369273283,1-002-827-0311x681 

Luis,Martin,Air cabin crew,[email protected],6154 James Cove,Christianberg,New York,RI,37208,JCB 15 digit,378433042568763,+42(0)3011107909 

Jose,Jones,Make,[email protected],431 Jessica Pass,East Robertburgh,Texas,SC,46458,Mastercard,4800941995105607,(047)981-1856x1825 

Mary,Pope,Field seismologist,[email protected],00799 Tracy Trace,Robinburgh,Rhode Island,HI,68855,JCB 16 digit,6011260007331949,+66(4)4995888616 

Jennifer,Villanueva,Tax adviser,[email protected],271 Simmons Mountains,Boydmouth,Nebraska,NM,98981,JCB 16 digit,210077713575961,639.575.1338x414 

額外的空間,我說他們在這裏爲了提高可讀性。

現在,項目的下一個部分是開發一個應用程序,使用Java將CSV文本文件中的非結構化數據導入規範化數據庫。

我有一些Java代碼,但它不工作的方式,我認爲它應該(隨便糾正我的想法應該如何完成)。我想它應該工作的方式是

  1. 將數據導入的Java
  2. 使用事先準備好的聲明函數來創建一個SQL語句,將數據導入到表與.setString功能沿都需要它

但是,它不能正常工作。首先,我會把代碼,然後我會解釋這是怎麼回事:

package com.company; 

import java.io.File; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Main { 

    private static Connection connect() { 
     Connection mysql = null; //sets values to null before actually attempting a connection 
     PreparedStatement pst = null; 
     ResultSet data = null; 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      String connectionStringURL = "jdbc:mysql://us-cdbr-azure-west-b.cleardb.com:3306/acsm_0a00c1270f36f77"; //database name 
      mysql = DriverManager.getConnection(connectionStringURL, "username", "password"); //username, password 

      if (mysql == null) //check to make sure that it actually connected 
       System.out.println("Connection Failed"); 
      else 
       System.out.println("Success"); 

     } 
     catch (Exception ex) //catches connection failure exception 
     { 
      ex.printStackTrace(); 
     } 
     return mysql; 
    } 

    public static void main(String[] args) throws Exception { 
     String filename = "/Desktop/myfile.txt"; 

     PreparedStatement pstmt = null; 
     Connection mysql = connect(); 
     try 
     { 
      pstmt = mysql.prepareStatement("INSERT INTO Customer (First Name, Last Name, Job, Email, Phone Number) VALUES (?,?,?,?,?)"); 

      Scanner s = new Scanner(new File("/Desktop/myfile.txt")); 
     ArrayList<String> list = new ArrayList<String>(); 
      while (s.hasNextLine()) 
      { 
       list.add(s.nextLine()); 
      } 
      System.out.println(list); 
      s.close(); 
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

第一個功能是很明顯我的數據庫連接功能,也沒有問題。

第二個函數是問題所在。目前,我已經準備好了一個已經包含SQL腳本的語句。但是,這還沒有被實際使用。我第一次嘗試完成的是逐行讀取文件行,然後對每個字段進行解析。

我問我的一個朋友怎麼辦呢,他對

說,通過線
  1. 讀取文件中的行,分裂的逗號和傳遞到陣列
  2. 做出都有每一列元素的數據類在文件
  3. 每一行的陣列中創建,創建數據類的一個對象
  4. 查找batchExecute()爲JDBC
  5. 寫INSERT語句和執行

對於第一步,它說「傳遞給數組」,是否只使用一個數組或ArrayList,對於任何一個,這是否意味着每個記錄/行都有它自己的數組/列表?

我不確定如何去做其他步驟。我一直在尋找互聯網上的答案,但我已經短暫。

我不認爲我忘記提及任何東西,但如果您需要對我所說的某些內容做更多的說明,我會很樂意嘗試解釋我的意思。任何幫助表示讚賞。

非常感謝你提前。

回答

1

我建議使用csv解析器讀取或寫入csv文件。下面是一個使用opencsv

import com.opencsv.CSVReader; 
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.util.ArrayList; 
import java.util.List; 

public class NewClass1 { 
    public static void main(String[] args) { 
     try { 

      String fileName = "yourfile.csv"; 
      List<String[]> customerList = readWholeCsvFile(fileName); 
      Connection conn = getConnection(); 
      persistWithOutDataClass(conn,customerList); 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } catch (SQLException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public static List<String[]> readWholeCsvFile(String fileName) throws IOException{ 
     List<String[]> myEntries = new ArrayList<>(); 
     CSVReader reader = new CSVReader(new FileReader(fileName), ',' ,'\'', 1); 
     myEntries = reader.readAll(); 
     return myEntries;   
    } 

    public static List<Customer> readCsvFileLineByLine(String fileName) throws IOException{ 
     List<Customer> customerList = new ArrayList<>(); 
     String [] nextLine; 
     CSVReader reader = new CSVReader(new FileReader(fileName), ',' ,'\'', 1); 
     while ((nextLine = reader.readNext()) != null) {   
      customerList.add(new Customer(nextLine[0], nextLine[1], nextLine[2], nextLine[3], nextLine[4])); 
     }  
     return customerList;   
    } 

    private static Connection getConnection() { 
     Connection conn = null; //sets values to null before actually attempting a connection    
     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      String connectionStringURL = "jdbc:mysql://us-cdbr-azure-west-b.cleardb.com:3306/acsm_0a00c1270f36f77"; //database name 
      conn = DriverManager.getConnection(connectionStringURL, "username", "password"); //username, password 
      if (conn == null) //check to make sure that it actually connected 
       System.out.println("Connection Failed"); 
      else 
       System.out.println("Success"); 
     } 
     catch (Exception ex){ 
      ex.printStackTrace(); 
     } 
     return conn; 
    } 

    private static void persistWithOutDataClass(Connection conn, List<String[]> customerList) throws SQLException{ 
     String insertStatement = " insert into Customer (First Name, Last Name, Job, Email, Phone Number) values (?, ?, ?, ?, ?)"; 
     PreparedStatement preparedStmt = conn.prepareStatement(insertStatement); 
     for(String[] row : customerList){ 
      preparedStmt.setString (1, row[0]); 
      preparedStmt.setString (2, row[1]); 
      preparedStmt.setString (3, row[2]); 
      preparedStmt.setString (4, row[3]); 
      preparedStmt.setString (5, row[11]); 
      preparedStmt.addBatch(); 
     } 
     preparedStmt.executeBatch(); 
    } 

    private static void persistWithDataClass(Connection conn, List<Customer> customerList) throws SQLException{ 
     String insertStatement = " insert into Customer (First Name, Last Name, Job, Email, Phone Number) values (?, ?, ?, ?, ?)"; 
     PreparedStatement preparedStmt = conn.prepareStatement(insertStatement); 
     for(Customer cust : customerList){ 
      preparedStmt.setString (1, cust.getFirstName()); 
      preparedStmt.setString (2, cust.getLastName()); 
      preparedStmt.setString (3, cust.getJob()); 
      preparedStmt.setString (4, cust.getEmail()); 
      preparedStmt.setString (5, cust.getPhone()); 
      preparedStmt.addBatch(); 
     } 
     preparedStmt.executeBatch(); 
    } 
} 

如果你想使用一個數據類的,你需要像下面

public class Customer { 

    private String firstName; 
    private String lastName; 
    private String job; 
    private String email; 
    private String phone; 

    public Customer(String firstName, String lastName, String job, String email, String phone) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.job = job; 
     this.email = email; 
     this.phone = phone; 
    } 

    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 getJob() { 
     return job; 
    } 

    public void setJob(String job) { 
     this.job = job; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 
} 
一個類的實例