我已经编写了程序。如何将第一列映射到“Slno”,将第二列映射到“COM_BUS”。因为,csv 文件不包含与表一样的正确标题。示例:
CSV 文件标题具有“sln”“C_BUS”
表“业务”有列有“SLNO”“COMBUS”。
我的问题是将数据导入 db2 表时不应该关注 csv 文件头。请帮忙。
私有静态最终
String SQL_INSERT = "INSERT INTO OPTYMGT.${table}(${keys}, RUN_TS)
VALUES(${values}, current_timestamp)";
private static final String TABLE_REGEX = "\\$\\{table\\}";
private static final String KEYS_REGEX = "\\$\\{keys\\}";
private static final String VALUES_REGEX = "\\$\\{values\\}";
private char seprator= ',';
public boolean loadCSV(Connection connection,String csvFile, String tableName) throws Exception {
boolean result = true;
CSVReader csvReader = null;
if(connection == null) {
throw new Exception("Not a valid connection.");
}
try {
csvReader = new CSVReader(new FileReader(csvFile), this.seprator);
//System.out.println("csvReader" +csvReader);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error occured while executing file. "
+ e.getMessage());
}
String[] headerRow = csvReader.readNext();
//System.out.println("headerRow" +headerRow);
if (null == headerRow) {
throw new FileNotFoundException(
"No columns defined in given CSV file." +
"Please check the CSV file format.");
}
String questionmarks = StringUtils.repeat("?,", headerRow.length);
questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
query = query.replaceFirst(VALUES_REGEX, questionmarks);
//System.out.println("Query: " + query);
/* SimpleDateFormat cDate = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date now = new Date();
String ccDate = cDate.format(now);
System.out.println("ccdate" +ccDate);*/
String[] nextLine;
Connection con = null;
PreparedStatement ps = null;