我在h2中使用CSVREADER来处理CSV文件,这非常棒。
final String SQL = "SELECT name, age, address FROM CSVREAD('/home/testuser/csvfile.csv', null, 'fieldSeparator=,')";
DriverManager.registerDriver(new org.h2.Driver());
try (Connection c = DriverManager.getConnection("jdbc:h2:" + filename, "sa", "");
PreparedStatement statement = c.prepareStatement(SQL);
ResultSet r = statement.executeQuery()) {
while (r.next()) {
// processing data...
}
LOGGER.debug("Processed data");
}
当此代码运行时,它会创建临时文件(csvfile.csv.mv.db和csvfile.csv.trace.mv.db)。是否有一个优雅的文件可以让H2自行清理,或者我是否需要手动完成它?
是否有可以设置的属性?
答案 0 :(得分:1)
您可以使用CSV tool directly, without opening a database:
Csv csv = new Csv();
csv.setOptions("fieldSeparator=,");
String fileName = "/home/testuser/csvfile.csv";
ResultSet rs = csv.read(fileName, null, null);
while (rs.next()) {
// processing data...
}
rs.close();