2016-11-06 69 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; 
    } 
} 
一个类的实例