sqlload import large amounts of data does have a number of advantages, it is a good tool.
1 public class OracleLoadDataHelperUntil { 2 3 private static String databaseUrl = ProperUtil.getValue("db.properties", "jdbc.url"); 4 5 private static String user = ProperUtil.getValue("db.properties", "jdbc.username"); 6 7 private static String pwd = ProperUtil.getValue("db.properties", "jdbc.password"); 8 9 private static String driver = ProperUtil.getValue("db.properties", "jdbc.driver"); 10 11 private static String fileRoute = ""; 12 13 /** 14 * 链接数据库地址 15 */ 16 private static String baseUrl = ""; 17 18 public OracleLoadDataHelperUntil() { 19 20 } 21 22 static { 23 try { 24 String url = databaseUrl.substring(databaseUrl.indexOf("@")); 25 baseUrl = url.replaceAll("@", "//").substring(0, url.lastIndexOf(":")+1) + "/" 26 Url.substring + (url.lastIndexOf ( ":") +. 1 ); 27 the Class.forName (Driver); 28 } the catch (a ClassNotFoundException E) { 29 e.printStackTrace (); 30 } 31 is } 32 33 is / ** 34 is * Get the name of the database field in accordance with table 35 * 36 * @param tableName 37 [ * @return 38 is * @throws Exception 39 * / 40 Private static List<String> getFieldsByTableName(String tableName) throws Exception { 41 List<String> fields = Lists.newArrayList(); 42 Connection conn = DriverManager.getConnection(databaseUrl, user, pwd); 43 String sql = "select COLUMN_NAME from USER_TAB_COLUMNS where TABLE_NAME=upper('" + tableName + "')"; 44 Statement stmt = conn.createStatement(); 45 ResultSet rs = stmt.executeQuery(sql); 46 String filed = ""; 47 while (rs.next()) { 48 = rs.getString Filed ( "COLUMN_NAME" ); 49 fields.add (Filed); 50 } 51 is rs.Close (); 52 is conn.Close (); 53 is return Fields; 54 is } 55 56 is / ** 57 is * Get Control named string 58 * 59 * @param fileName import file name, the full path 60 * @param tableName table 61 is * @param DELIMITER delimiter 62 is * @return 63 is * / 64 Private static String getStrCtr (fileName String, String tableName, String DELIMITER, List <String> the fileds) { 65 the StringBuffer SB = new new the StringBuffer (); 66 String LineSeparator the System.getProperty = ( "line.separator, and is" ); 67 // 0 is 1 starting from the first row from the second row 68 sb.append ( "the OPTIONS (Skip = 0)" ) .append (LineSeparator); 69 // load the data file 70 sb.append ( "the lOAD the dATA" ) .append ( LineSeparator); 71 is // prevent distortion Chinese 72 sb.append ( "CHARACTERSET ZHS16GBK" ) .append (LineSeparator); 73 is sb.append ( "INFILE") .append ( "\ '") the append (fileName) .append ( "\.'" ) .Append (LineSeparator); 74 IF (Strings.isStringEmpty (DELIMITER)) { 75 SB. the append ( "\" STR X'170A '\ "" ) .append (LineSeparator); 76 } 77 // overwritten 78 sb.append ( "the APPEND the INTO TABLE" ) .append (tableName) .append (LineSeparator); 79 sb.append ( "the REPLACE" ) .append (LineSeparator); 80 // data per line recording "," separated, TERMINATED for controlling the field separator may be a plurality of characters. 81 IF (Strings. sb.append("FIELDS TERMINATED BY X'lib'").append(lineSeparator); 83 }else { 84 sb.append("FIELDS TERMINATED BY '"+delimiter+"'").append(lineSeparator); 85 } 86 // 将数据写入对应的字段 87 sb.append("trailing nullcols ( ").append(lineSeparator); 88 sb.append(getFieldsStr(fileds)).append(lineSeparator); 89 sb.append(")"); 90 91 return sb.toString(); 92 } 93 94 95 private static String getFieldsStr (List <String> the fileds) { 96 StringJoiner SJ = new new StringJoiner ( "," + the System.getProperty ( "line.separator, and is" )); 97 for (S String: the fileds) { 98 sj.add (S) ; 99 } 100 return sj.toString (); 101 } 102 103 / ** 104 * Get the path control file 105 * 106 * @param fileRoute data file address path 107 * @param fileName data file name 108 *@param tableName table 109 * @param DELIMITER separator 110 * @param the fieldNames set of attributes 111 * @param ctlfileName by filename 112 * @return 113 * / 114 Private static String getCtlFileUrl (fileRoute String, String fileName, tableName String, String DELIMITER, 115 List <String> the fieldNames, String ctlfileName) { 1 16 String ctlFileUrl = "" ; 117 FileWriter FW = null ; 1 18 the try { 119 String strctl = getStrCtr(fileRoute + fileName, tableName, delimiter, fieldNames); 120 fw = new FileWriter(fileRoute + "" + ctlfileName); 121 fw.write(strctl); 122 ctlFileUrl = fileRoute + "" + ctlfileName; 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } finally { 126 try { 127 fw.flush(); 128 fw.close(); 129 } The catch (IOException E) { 130. e.printStackTrace (); 131 is } 132 } 133 return ctlFileUrl; 134 } 135 136 / ** 137 * determine what system, true of windows, false as Linux 138 * 139 * @return 140 * / 141 is Private static Boolean isOsWin () { 142 Boolean In Flag = Boolean.FALSE The; 143 String the System.getProperty OS = ( "The os.name" ); 144 IF(. os.toLowerCase () startsWith ( "win" )) { 145 In Flag = of Boolean.TRUE; 146 } 147 return In Flag; 148 } 149 150 / ** 151 * 152 * @param fileRoute file path 153 * @param ctlfileName control file path 154 * @param logfilename log file path 155 * @param Flag whether to execute immediately, true execute the command, false is not executed 156 * @return sqlload execution statement 157 * / 158 private static String excuteLoadData(String fileRoute, String ctlfileName, String logfileName, boolean flag) { 159 InputStream ins = null; 160 Process process = null; 161 String dos = "sqlldr " + user + "/" + pwd + "@" + baseUrl + " control=" + fileRoute + "" + ctlfileName + " log=" 162 + fileRoute + "" + logfileName; 163 System.out.println("dos:" + dos); 164 if(flag==false) { 165 return dos; 166 } 167 try { 168 if (isOsWin().equals(Boolean.FALSE)) { 169 process = Runtime.getRuntime().exec(dos); 170 } else { 171 String[] cmd = new String[] { "cmd.exe", "/C", dos }; 172 process = Runtime.getRuntime().exec(cmd); 173 } 174 ins = process.getInputStream(); 175 Charset charset=Charset.forName("GBK"); 176 BufferedReader reader = new BufferedReader(new InputStreamReader(ins,charset)); 177 String line = null; 178 while ((line = reader.readLine()) != null) { 179 String msg = new String(line.getBytes(), "GBK"); 180 System.out.println(line); 181 } 182 int exitValue = process.waitFor(); 183 if (exitValue == 0) { 184 System.out.println("返回值:" + exitValue + "\n数据导入成功"); 185 186 } The else { 187 System.out.println ( "Return Value:" + exitValue + "\ n Data import failed" ); 188 } 189 . Process.getOutputStream () Close (); 190 } the catch (Exception E) { 191 E .printStackTrace (); 192 } 193 return DOS; 194 } 195 196 / ** 197 * 198 * execute data import method sqlload 199 * 200 is * @param filePath root file 201 * @Param fileName data file .dat file 202 * @param tableName table 203 * @param the fieldNames attribute field 204 * @param In Flag whether immediately, true Run, false not performed 205 * @return sqlload execution statement 206 * @ throws Exception 207 * / 208 209 public static String excuteSqlLoadImportData (filePath String, String fileName, tableName String, String DELIMITER, List <String> Fields, Boolean In Flag) throws Exception { 210 String str = ""; 211 String ctlfileName = tableName + ".ctl"; 212 String logfileName = tableName + ".log"; 213 if (Strings.isStringEmpty(filePath)) { 214 filePath = fileRoute; 215 } 216 if(CollectionUtils.isEmpty(fields)) { 217 fields=getFieldsByTableName(tableName); 218 } 219 String fileUrl = getCtlFileUrl(filePath, fileName, tableName, delimiter, fields, ctlfileName); 220 if (!Strings.isStringEmpty(fileUrl)) { 221 str = excuteLoadData(filePath, ctlfileName, logfileName, flag); 222 } 223 return str; 224 } 225 226 }