-1

所以我想每读 100 行并打印它,它应该每 100 行发生一次,我不知道在哪里插入该代码。包含一百万条记录的 CSV 文件没有被插入到数据库中,因为只有几千条记录被插入。

String csvFilePath = "C:\\Student1.csv";
try {
    BufferedReader lineReader = new BufferedReader(new FileReader("C:\\File12\\Student1.csv"));
    CSVParser records = CSVParser.parse(lineReader, CSVFormat.EXCEL.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
    System.out.println(records.size);
    ArrayList<TestSql> students = new ArrayList<TestSql>();
    for (CSVRecord record : records) {
        TestSql testsql = new TestSql();
        testsql.setDate(record.get(0));
        testsql.setName(record.get(1));
        testsql.setGender(record.get(2));

        students.add(testsql);
    }
    PreparedStatement statement = null;
    Connection con = dbconnection();
    String sql = "INSERT INTO test12(DOB, NAME, GENDER) VALUES (?, ?, ?)";
    statement = con.prepareStatement(sql);
    for (TestSql record : students) {
        statement.setString(1, record.getDate());
        statement.setString(2, record.getName());
        statement.setString(3, record.getGender());
        statement.addBatch();
    }
    statement.executeBatch();
    con.commit();
    con.close();

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

public static Connection dbconnection() {
    Connection connection = null;
    try {
        System.out.println( "Hello World!" );
        Class.forName("com.mysql.cj.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/newschema1", "root", "12345");
        System.out.println("connection sucessfull");
        connection.setAutoCommit(false);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return connection;
}
4

1 回答 1

1

如果要将 CSV 文件中的记录以 100 条为一组插入到数据库表中,则需要一个计数器。在下面的代码中,我使用了一个变量count。每当它达到 100 时,代码就会插入这 100 行并重置count变量。

注意:代码后有更多解释。

import java.io.BufferedReader;
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 org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

public class CsvParse {
    private static final int  LIMIT = 100;

    public static Connection dbConnection() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/newschema1",
                                                            "root",
                                                            "12345");
        connection.setAutoCommit(false);
        return connection;
    }

    public static void main(String[] args) {
        try (BufferedReader lineReader = new BufferedReader(new FileReader("C:\\File12\\Student1.csv"))) {
            CSVParser records = CSVParser.parse(lineReader,
                                                CSVFormat.EXCEL.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
            String sql = "INSERT INTO test12(DOB, NAME, GENDER) VALUES (?, ?, ?)";
            Connection con = dbConnection();
            PreparedStatement statement = con.prepareStatement(sql); 
            int count = 0;
            for (CSVRecord record : records) {
                count++;
                if (count > LIMIT) {
                    count = 1;
                    statement.executeBatch();
                    con.commit();
                    statement.clearBatch();
                }
                statement.setString(1, record.get(0));
                statement.setString(2, record.get(1));
                statement.setString(3, record.get(2));
                statement.addBatch();
            }
            // Insert last batch that may be less than LIMIT.
            statement.executeBatch();
            con.commit();
            con.close();
            records.close();
        }
        catch (IOException | SQLException e) {
            e.printStackTrace();
        }
    }
}

在方法dbConnection()中,我删除了,Class.forName()因为不再需要它。我还更改了异常处理。如果该方法未能获得数据库连接,那么继续没有多大意义,因为您将无法向数据库中插入任何内容,这就是程序的全部意义所在。因此,捕获SQLExceptionin 方法dbConnection()并打印堆栈跟踪意味着当您尝试创建 a 时PreparedStatement,您将得到 aNullPointerExcetion因为con将为空。

在方法中,我在创建时main使用try-with-resourceslineReader

我看不出上课的原因TestSql。您可以PreparedStatement直接从 CSV 记录中简单地设置参数。

由于 Java 7 有多重捕获,因此当每个块只打印堆栈跟踪时,不需要catch为每个异常单独的块。catch

于 2021-02-19T14:14:35.810 回答