0
我編寫了程序。如何將第一列映射到「Slno」,第二列映射到「COM_BUS」。因爲csv文件沒有包含與表一樣的正確標題。 實施例:
CSV文件頭具有 「SLN」 「C_BUS」如何在導入數據到db時將csv文件的標題名稱映射到db表格
表 「商務」 具有列有 「SLNO」 「COMBUS」。
我的問題是csv文件標題不應該是將數據導入到db2表時所關心的問題。請幫忙。
私有靜態最後
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;
打完共享代碼,你說的這個程序可以讀取來自CSV的第一行的列名,但要忽略第一行,並使用不同的欄柱名字呢?如果是這樣:你知道列名,並且可以在SQL中對它們進行硬編碼,或者你想查看數據庫並在運行時找出列名? –
你有沒有原因試圖重寫'IMPORT'和'LOAD'工具? –