所以我想读取每 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;
}
答案 0 :(得分: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()
,因为它是 no longer needed。我还更改了异常处理。如果该方法无法获得数据库连接,那么继续操作就没有多大意义,因为您将无法向数据库中插入任何内容,这就是程序的全部意义所在。因此,在方法 SQLException
中捕获 dbConnection()
并打印堆栈跟踪意味着当您尝试创建 PreparedStatement
时,您将获得 NullPointerExcetion
,因为 con
将为空。
在方法 main
中,我在创建 lineReader
时使用 try-with-resources。
我没有看到 TestSql
类的原因。您可以直接从 CSV 记录中简单地设置 PreparedStatement
参数。
由于 Java 7 有 multi-catch,所以当每个 catch
块只是打印堆栈跟踪时,不需要为每个异常单独的 catch
块。