1. 自己太菜,基本的东西搞的自己团团转,谨记。
原因: 在导入txt到表中,一直提示不对应。经过百度,是因为oracle数据库的date在存储的时候需要格式化一下。
因为txt中数据量较大,所以采用jdbc的批处理。
1.拼sql
String insertStr = "insert into " + tempTableName + " (" + colname + ") VALUES("; //colname就是 txt中参数列名 这里最好是与oracle表中的列名对应
//上面就有 处理date的解决方法 当时百度的答案 只是单条语句 直接加在值上处理,我脑子犯晕,想着 ? 代替值的这种该怎么办呢。原来TM是凉拌。for (int i = 0; i < colname.split(",").length; i++) { //特定的列 需要date处理 if(i == 12 || i == 17 || i == 19){ insertStr += "to_date( ?,'yyyy-mm-dd HH24:MI:SS'),"; }else{ insertStr += "?,"; } } insertStr = insertStr.substring(0, insertStr.length() - 1); insertStr += " )";
// 问好 ? 还是 ? ,只不过在外层加了个 to_date(?,'yyyy-mm-dd HH24:MI:SS') 这种格式,在拼接sql的时候搞定。2.获得连接
注意:
一个流 在被循环之后,就不能用了。因为指针的原因InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); //考虑到编码格式
//获得连接 conn = this.getJdbcTemplate().getDataSource().getConnection(); //关闭自动提交 conn.setAutoCommit(false); Long batchCount = 1L; if ("ora".equals(StaticConstants.getDatabaseType())) { this.update1("alter table " + tempTableName + " NOLOGGING"); //关闭表日志 } else if ("mysql".equals(StaticConstants.getDatabaseType())) { } pstmt = conn.prepareStatement(insertStr); //上一篇文章里有下面这个获取txt中的行数,来进行特定的操作。 注意:1 int totalNub = this.getFileLineNub(target); // 文件 File file = new File(target); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); //考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; int lineNub = 1; while ((lineTxt = bufferedReader.readLine()) != null) { if (lineNub > 5) { //如果到了最后4行 跳出循环不进行导入操作 if (totalNub - lineNub <= 4) { break; } if (batchCount % MaxCommit == 0) { pstmt.executeBatch(); conn.commit(); pstmt.clearBatch(); System.out.println("提交数据量:" + batchCount + " " + DateUtil.getCurrentDateTime()); } String[] values = lineTxt.split("\t"); String[] cvalues = colname.split(","); int m = 1; String dsdm = ""; for (int i = 0; i < cvalues.length; i++) { String str = ""; if (i == 23) { str = values[i].toString().trim(); str = str.toUpperCase(); if (str.length() < 4) { str = "0" + str; } pstmt.setObject(m++, str); dsdm = str.substring(0, 2); } else if (i == 25) { //截取地市代码dsdm的值 pstmt.setObject(m++, dsdm); } else if (i == cvalues.length - 1) { pstmt.setObject(m++, "1"); } else if (i == 13) { str = values[i].toString().trim(); str = str.toUpperCase(); if ("".equals(str)) { pstmt.setObject(m++, str); } else { String desphone = PhoneUtil.getEncryptPhone(str); pstmt.setObject(m++, desphone); } } else { str = values[i].toString().trim(); str = str.toUpperCase(); pstmt.setObject(m++, str); } } pstmt.addBatch(); batchCount++; insertTempRecords++; } lineNub++; } pstmt.executeBatch(); conn.commit(); pstmt.clearBatch(); System.out.println("提交剩余数据:" + (batchCount - 1) + " " + DateUtil.getCurrentDateTime()); read.close(); } else { //没找到 }