我有一个管道分隔的文件(excel.xlsx(,需要对其进行解析以查找某些数据。数据都在列A中。第一行有日期,最后一行有行计数,中间的所有数据都是行数据。我想从标题中提取每行的前三个字段和日期,并将其存储到H2表中。我的文件中每行都有额外的数据。我需要帮助创建解析文件并将其插入数据库的代码。我写了一个实体和一些代码,但现在被卡住了。
我的文件
20200310|
Mn1223|w01192|windows|extra|extra|extra||
Sd1223|w02390|linux|extra|extra|extra||
2
我的桌子
DROP TABLE IF EXISTS Xy_load ;
CREATE TABLE Xy_load (
account_name VARCHAR(250) NOT NULL,
command_name VARCHAR(250) NOT NULL,
system_name VARCHAR (250) NOT NULL,
CREATE_DT date (8) DEFAULT NULL
);
实体类
public class ZyEntity {
@Column(name="account_name")
private String accountName;
@Column(name="command_name")
private String commandName;
@Column(name="system_name")
private String systemName;
@Column(name="CREATE_DT")
private int createDt;
public ZyEntity(String accountName, String commandName, String systemName){
this.accountName=accountName;
this.commandName=commandName;
this.systemName=systemName;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getCommandName() {
return commandName;
}
public void setCommandName(String commandName) {
this.commandName = commandName;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
public int getCreateDt() {
return createDt;
}
public void setCreateDt(int createDt) {
this.createDt = createDt;
}
}
我在的帮助下找到了答案
List<DataToInsert> parseData(String filePath) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(filePath));
// remove date and amount
lines.remove(0);
lines.remove(lines.size() - 1);
return lines.stream()
.map(s -> s.split("[|]")).map(val -> new DataToInsert(val[0], val[1], val[2])).collect(Collectors.toList());
}
public void insertZyData(List<ZyEntity> parseData) {
String sql = "INSERT INTO Landing.load (account_name,command_name,system_name)"+
"VALUES (:account_name,:command_name,:system_name)";
for (ZyEntity zyInfo : parseData){
SqlParameterSource source = new MapSqlParameterSource("account_name", zInfo.getAccountName())
.addValue("command_name", zyInfo.getCommandName())
.addValue("system_name", zyInfo.getSystemName());
jdbcTemplate.update(sql, source);
}
}