Java CSV转数据库表
在现代软件开发中,很常见需要将CSV(逗号分隔值)文件的数据导入到数据库表中。CSV是一种简单的文件格式,其中每行表示一条记录,每个字段由逗号分隔。而数据库表则是用于存储、管理和查询数据的结构化数据集合。
本文将介绍如何使用Java将CSV文件转换为数据库表,并提供代码示例。
步骤一:导入依赖库
首先,我们需要导入Java CSV库,以便能够处理CSV文件。在本示例中,我们将使用Apache Commons CSV库,它是一个流行的Java库,用于读写CSV文件。
你可以通过Maven或Gradle等构建工具将该库添加到你的项目中。以下是Maven配置的示例:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
步骤二:读取CSV文件
在开始转换之前,我们需要读取CSV文件中的数据。使用Apache Commons CSV库可以轻松地实现这一点。以下是一个示例代码:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
public List<String[]> readCSV(String filePath) throws IOException {
List<String[]> records = new ArrayList<>();
Reader reader = new FileReader(filePath);
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
for (CSVRecord csvRecord : csvParser) {
String[] record = new String[csvRecord.size()];
for (int i = 0; i < csvRecord.size(); i++) {
record[i] = csvRecord.get(i);
}
records.add(record);
}
csvParser.close();
return records;
}
}
在上面的代码中,我们使用CSVParser
类来解析CSV文件,并将每行的记录存储在一个List
中。
步骤三:创建数据库表
在将CSV数据插入数据库之前,我们需要先创建数据库表。以下是一个示例代码,用于创建一个名为employees
的表:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseTableCreator {
public void createTable() {
Connection connection = null;
Statement statement = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
statement = connection.createStatement();
String sql = "CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50), age INT)";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在上面的代码中,我们使用Java的JDBC(Java Database Connectivity)API来连接数据库,并使用Statement
对象执行SQL语句来创建表。
步骤四:插入数据
现在我们已经准备好了CSV数据和数据库表,下一步是将CSV数据插入到数据库表中。以下是一个示例代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
public class DataInserter {
public void insertData(List<String[]> data) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
String sql = "INSERT INTO employees (id, name, age) VALUES (?, ?, ?)";
preparedStatement = connection.prepareStatement(sql);
for (String[] record : data) {
preparedStatement.setInt(1, Integer.parseInt(record[0]));
preparedStatement.setString(2, record[1]);
preparedStatement.setInt(3, Integer.parseInt(record[2]));
preparedStatement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在上面的代码中,我们使用PreparedStatement
对象执行插入数据的SQL语句,通过循环遍