如何使用Java中的CSVReader对象读取CSV文件?

时间:2018-07-20 17:27:04

标签: java mysql jdbc opencsv

这是我的代码,我不断收到classnotfound异常。我也已经导入并构建了opencsv-4.2 jar文件的路径。

以下是“ Customer_Info.csv”中的示例记录

10001   MICHELLE    VILLEGAS3   Savings SRINATH MICHELLE.VILLEGAS3@NOBODY.COM   Y   07-10-18    1050 WEST FIFTH STREET      AZUSA   IND 917023308

阅读器类

import java.sql.SQLException;
import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
public class DataLoader {
    private static final String SAMPLE_CSV_FILE_PATH = "./Customer_info.csv";

    public static void main (String[] args) throws SQLException, IOException {

        ManageDBResource.createConnection();
        try (
                Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
                CSVReader csvReader = new CSVReader(reader);
         ) {
                    // Reading Records One by One in a String array
              String[] nextRecord;

              while ((nextRecord = csvReader.readNext()) != null) {
              System.out.println("Cust_ID : " + nextRecord[0]);
              System.out.println("First_Name : " + nextRecord[1]);
              System.out.println("Last_Name : " + nextRecord[2]);
              System.out.println("User Type : " + nextRecord[3]);
              System.out.println("==========================");
              }
         }
    }
}

ManageDBResource类具有以下代码: DBUtil只是一个用于将连接对象返回到数据库的类。

import java.sql.Connection;
import java.sql.SQLException;

public class ManageDBResource {         //To createConnection to DB

    static Connection conn = null;
    public static void createConnection() throws SQLException {

        try {

            conn = DBUtil.getConnection(DBType.MYSQLDB);
            System.out.println("Connection established to MySql DB");
        }
        catch(SQLException e) {
            DBUtil.showErrorMessage(e);
            e.printStackTrace();
        }
        finally {
            conn.close();
        }
    }
}

我得到的错误。

Fri Jul 20 22:41:35 IST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Connection established to MySql DB
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/ObjectUtils
    at com.opencsv.CSVParser.<init>(CSVParser.java:209)
    at com.opencsv.CSVReader.<init>(CSVReader.java:197)
    at com.opencsv.CSVReader.<init>(CSVReader.java:179)
    at com.opencsv.CSVReader.<init>(CSVReader.java:131)
    at com.opencsv.CSVReader.<init>(CSVReader.java:71)
    at DataLoader.main(DataLoader.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.ObjectUtils
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 6 more

0 个答案:

没有答案