只是一个例子,方便以后查阅。

  1. import ey.db.oracle.OracleHelper;
  2. import ey.db.type.*;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileFilter;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. //import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.net.URLDecoder;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.sql.PreparedStatement;
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.StringTokenizer;
  23. import java.util.regex.Pattern;
  24. import java.util.regex.Matcher;
  25.  
  26. import oracle.jdbc.OracleTypes;
  27.  
  28. public class ImpCNetData {
  29. static Configuration conf=new Configuration("/ProjectConfig.properties");
  30. static String db_host=conf.getValue("cdma_host");
  31. static String db_user=conf.getValue("cdma_user");
  32. static String db_passwd=conf.getValue("cdma_passwd");
  33. static String impFilePath=conf.getValue("ImpFilePath");
  34. static String impFileLog=conf.getValue("ImpFileLog");
  35. static String lastKeepString=conf.getValue("LastKeepString");
  36. static String lastFilterString=conf.getValue("LastFilterString");
  37. static String connStr="";
  38. static Pattern lastFilterRex=null;
  39. static Pattern lastKeepRex=null;
  40. private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
  41. //批量插入数据
  42. public void InsertDataBatch(List<CNetData> cnds) throws Exception
  43. {
  44. Connection conn=null;
  45. try {
  46. Class.forName(oracleDriverName);
  47. conn = DriverManager.getConnection(db_host, db_user, db_passwd);
  48. conn.setAutoCommit(false);
  49. String sql = "insert into cn_visit(visitid, mobile, url, visittime, onlinetime, desip, desport, mobileip, mobileport,urltypeid, comefrom, bsid, username,housecityid) VALUES(seq_cn_visitid.Nextval,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  50. PreparedStatement prest = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  51.  
  52. for(int x = 0; x < cnds.size(); x++){
  53. CNetData cnd=cnds.get(x);
  54. prest.setString(1, cnd.getMobile());
  55. prest.setString(2, cnd.getUrl());
  56. prest.setString(3, cnd.getVisitTime());
  57. prest.setString(4, cnd.getOnlineTime());
  58. prest.setString(5,cnd.getDesIp());
  59. prest.setInt(6,cnd.getDesPort());
  60. prest.setString(7,cnd.getMobileIp());
  61. prest.setInt(8,cnd.getMobilePort());
  62. prest.setInt(9,cnd.getFileUrlId());
  63. prest.setInt(10,cnd.getComeFrom());
  64. prest.setString(11,cnd.getBsid());
  65. prest.setString(12,cnd.getUsername());
  66. prest.setInt(13,cnd.getHouseCityId());
  67. prest.addBatch();
  68. }
  69. prest.executeBatch();
  70. conn.commit();
  71. conn.close();
  72. } catch (SQLException ex) {
  73. ex.printStackTrace();
  74. } catch (ClassNotFoundException ex) {
  75. ex.printStackTrace();
  76. }
  77. catch(Exception ex)
  78. {
  79. ex.printStackTrace();
  80. }
  81. finally
  82. {
  83. if (conn!=null && !conn.isClosed())
  84. conn.close();
  85. }
  86. }
  87. //读取网址类型
  88. public DataTable GetUrlType()
  89. {
  90. try
  91. {
  92. OracleConnection conn= new OracleConnection(db_host+";"+db_user+";"+db_passwd);
  93. String cmd="FX114V01_CN_VISIT.GetUrlList";
  94. Parameter pds=new Parameter("out_data",OracleTypes.CURSOR,null, ParameterDirection.OUT);
  95. Parameter pcode=new Parameter("out_code",OracleTypes.INTEGER,null, ParameterDirection.OUT);
  96. DataSet ds=OracleHelper.ExecuteDataSet(conn,CommandType.StoreProcedure, cmd, pds,pcode);
  97. DataTable dt=ds.Tables[0];
  98. return dt;
  99. }
  100. catch(Exception ex)
  101. {
  102. ex.printStackTrace();
  103. }
  104. return null;
  105. }
  106. //导入数据
  107. public void ImpData() throws Exception
  108. {
  109. //StringBuffer sb=new StringBuffer();
  110. String tempstr=null;
  111. String url="";
  112. DataTable dt=GetUrlType();//URL分类表
  113. if(lastFilterString.length()>0 && lastFilterRex==null)
  114. lastFilterRex=Pattern.compile(lastFilterString);
  115. if(lastKeepString.length()>0 && lastKeepRex==null)
  116. lastKeepRex=Pattern.compile(lastKeepString);
  117. try
  118. {
  119. //网址类型不能为空
  120. if(dt!=null)
  121. {
  122. //String path="/usr/hadoop/bigdata/filterurl/part/";
  123. File dir=new File(impFilePath);
  124. File[] files=null;
  125. if(dir.isDirectory())
  126. files=dir.listFiles(new MyFileFilter());
  127. for(int i=0;i<files.length;i++)
  128. {
  129. //System.out.println(files[i].getName());
  130. List<CNetData> cnds=new ArrayList<CNetData>();
  131. FileInputStream fis=new FileInputStream(files[i]);
  132. BufferedReader br=new BufferedReader(new InputStreamReader(fis,"UTF-8"));
  133. //文件逐行读取
  134. while((tempstr=br.readLine())!=null)
  135. {
  136. try
  137. {
  138. StringTokenizer itr=new StringTokenizer(tempstr,"|");
  139. if(itr.hasMoreTokens() && itr.countTokens()==11)
  140. {
  141. CNetData cnd=new CNetData();
  142. String mobile=itr.nextToken().trim();
  143. if(mobile.length()>11)
  144. cnd.setMobile(mobile.substring(0,11));
  145. else
  146. cnd.setMobile(mobile);
  147. url=itr.nextToken().trim();
  148.  
  149. //替换URL特殊编码
  150. if(url.indexOf("%2F")>=0 || url.indexOf("%3F")>=0)
  151. {
  152. url = url.replaceAll("%2F","/");
  153. url = url.replaceAll("%3A",":");
  154. url = url.replaceAll("%20"," ");
  155. url = url.replaceAll("%3F","?");
  156. url = url.replaceAll("%3D","=");
  157. }
  158. //保留需要的
  159. if(lastKeepString.length()>0 && lastKeepRex!=null)
  160. {
  161. Matcher lastKeepMatcher=null;
  162. //防止引用或者搜索过来的
  163. if(url.length()<=30)
  164. lastKeepMatcher=lastKeepRex.matcher(url);
  165. else
  166. lastKeepMatcher=lastKeepRex.matcher(url.substring(0,30));
  167.  
  168. //没找到的话跳出本次循环
  169. if(!lastKeepMatcher.find())
  170. continue; //跳出本次循环
  171. }
  172. //过滤不需要的
  173. if(lastFilterString.length()>0 && lastFilterRex!=null)
  174. {
  175. Matcher lastMatcher=lastFilterRex.matcher(url);
  176. //找到需要过滤掉的字符串
  177. if(lastMatcher.find())
  178. continue; //跳出本次循环
  179. }
  180. cnd.setUrl(url);
  181. cnd.setVisitTime(itr.nextToken());
  182. cnd.setMobileIp(itr.nextToken());
  183. cnd.setDesIp(itr.nextToken());
  184. cnd.setMobilePort(Integer.parseInt(itr.nextToken().trim()));
  185. cnd.setDesPort(Integer.parseInt(itr.nextToken().trim()));
  186. cnd.setOnlineTime(itr.nextToken());
  187. cnd.setBsid(itr.nextToken());
  188. cnd.setUsername(itr.nextToken());
  189. cnd.setComeFrom(Integer.parseInt(itr.nextToken().trim()));
  190. cnd.setFileUrlId(0);//默认值
  191. //查找所属URL类型
  192. for(int k=0;k<dt.Rows.length;k++)
  193. {
  194. if(url.indexOf(dt.Rows[k].Columns[1].colValue.toString())>=0)
  195. {
  196. cnd.setFileUrlId(Integer.parseInt(dt.Rows[k].Columns[0].colValue.toString().trim()));
  197. break;
  198. }
  199. }
  200. //判断房源或者链接的城市
  201. /*
  202. 21 北京
  203. 51 东莞
  204. 52 珠海
  205. 55 佛山
  206. 56 广州
  207. 59 惠州
  208. 68 深圳
  209. 162 武汉
  210. 174 长沙
  211. 341 上海
  212. * */
  213. if(url.indexOf("sz.")>=0||url.indexOf("sz/")>=0)
  214. cnd.setHouseCityId(68);
  215. else if(url.indexOf("gz.")>=0||url.indexOf("gz/")>=0||url.indexOf("gz_")>=0)
  216. cnd.setHouseCityId(56);
  217. else if(url.indexOf("dg.")>=0||url.indexOf("dg/")>=0||url.indexOf("dg_")>=0)
  218. cnd.setHouseCityId(51);
  219. else if(url.indexOf("fs.")>=0||url.indexOf("fs/")>=0||url.indexOf("fs_")>=0)
  220. cnd.setHouseCityId(55);
  221. else if(url.indexOf("zh.")>=0||url.indexOf("zh/")>=0||url.indexOf("zh_")>=0)
  222. cnd.setHouseCityId(52);
  223. else if(url.indexOf("hz.")>=0||url.indexOf("hz/")>=0||url.indexOf("hz_")>=0)
  224. cnd.setHouseCityId(59);
  225. else if(url.indexOf("zs.")>=0||url.indexOf("zs/")>=0||url.indexOf("zs_")>=0)
  226. cnd.setHouseCityId(73);
  227. else if(url.indexOf("bj.")>=0||url.indexOf("bj/")>=0||url.indexOf("bj_")>=0)
  228. cnd.setHouseCityId(21);
  229. else if(url.indexOf("sh.")>=0||url.indexOf("sh/")>=0||url.indexOf("sh_")>=0)
  230. cnd.setHouseCityId(341);
  231. else if(url.indexOf("cs.")>=0||url.indexOf("cs/")>=0||url.indexOf("cs_")>=0)
  232. cnd.setHouseCityId(174);
  233. else if(url.indexOf("wh.")>=0||url.indexOf("wh/")>=0||url.indexOf("wh_")>=0)
  234. cnd.setHouseCityId(162);
  235. else
  236. cnd.setHouseCityId(0);
  237.  
  238. //上面的方式没找到的情况下才用下面的模式查找
  239. if(cnd.getHouseCityId()<=0)
  240. {
  241. if(url.contains("sz")||url.contains("shenzhen"))
  242. cnd.setHouseCityId(68);
  243. else if(url.contains("guangzhou"))
  244. cnd.setHouseCityId(56);
  245. else if(url.contains("dg")||url.contains("dongguan"))
  246. cnd.setHouseCityId(51);
  247. else if(url.contains("fs")||url.contains("foshan"))
  248. cnd.setHouseCityId(55);
  249. else if(url.contains("zhuhai"))
  250. cnd.setHouseCityId(52);
  251. else if(url.contains("hz")||url.contains("huizhou"))
  252. cnd.setHouseCityId(59);
  253. else if(url.indexOf("zs")>=0||url.indexOf("zhongshan")>=0)
  254. cnd.setHouseCityId(73);
  255. else if(url.indexOf("bj")>=0||url.indexOf("beijing")>=0)
  256. cnd.setHouseCityId(21);
  257. else if(url.indexOf("shanghai")>=0)
  258. cnd.setHouseCityId(341);
  259. else if(url.indexOf("cs")>=0||url.indexOf("changsha")>=0)
  260. cnd.setHouseCityId(174);
  261. else if(url.indexOf("wh")>=0||url.indexOf("wuhan")>=0)
  262. cnd.setHouseCityId(162);
  263. else
  264. cnd.setHouseCityId(0);
  265. }
  266. cnds.add(cnd);
  267. }
  268. }
  269. catch(Exception ex)
  270. {
  271. ex.printStackTrace();
  272. }
  273. }
  274. //System.out.println(cnds.size());
  275. //插入数据
  276. if(cnds.size()>0)
  277. {
  278. InsertDataBatch(cnds);
  279. writeLog(files[i].getName()+" " +cnds.size());
  280. files[i].delete();
  281. }
  282. br.close();
  283. fis.close();
  284. //break;
  285. }
  286. }
  287. }
  288. catch(IOException ex)
  289. {
  290. System.out.println(ex.getStackTrace());
  291. }
  292. }
  293. public static void writeLog(String str)
  294. {
  295. try
  296. {
  297. //String path="/usr/hadoop/bigdata/filterurl/importfile.log";
  298. File file=new File(impFileLog);
  299. if(!file.exists())
  300. file.createNewFile();
  301. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  302. FileOutputStream out=new FileOutputStream(file,true); //如果追加方式用true
  303. StringBuffer sb=new StringBuffer();
  304. //sb.append("-----------"+sdf.format(new Date())+"------------\n");
  305. sb.append(sdf.format(new Date())+" "+str+"\n");
  306. out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
  307. out.close();
  308. }
  309. catch(IOException ex)
  310. {
  311. System.out.println(ex.getStackTrace());
  312. }
  313. }
  314. public static void main(String[] args) throws Exception {
  315. try
  316. {
  317. ImpCNetData icd=new ImpCNetData();
  318. icd.ImpData();
  319. }
  320. catch(Exception ex)
  321. {
  322. ex.printStackTrace();
  323. }
  324. }
  325. }

记录类:

  1. public class CNetData {
  2. private String mobile;
  3. public String getMobile() {
  4. return mobile;
  5. }
  6. public void setMobile(String mobile) {
  7. this.mobile = mobile;
  8. }
  9. public String getUrl() {
  10. return url;
  11. }
  12. public void setUrl(String url) {
  13. this.url = url;
  14. }
  15. public String getVisitTime() {
  16. return visitTime;
  17. }
  18. public void setVisitTime(String visitTime) {
  19. this.visitTime = visitTime;
  20. }
  21. public String getOnlineTime() {
  22. return onlineTime;
  23. }
  24. public void setOnlineTime(String onlineTime) {
  25. this.onlineTime = onlineTime;
  26. }
  27. public String getDesIp() {
  28. return desIp;
  29. }
  30. public void setDesIp(String desIp) {
  31. this.desIp = desIp;
  32. }
  33. public int getDesPort() {
  34. return desPort;
  35. }
  36. public void setDesPort(int desPort) {
  37. this.desPort = desPort;
  38. }
  39. public String getMobileIp() {
  40. return mobileIp;
  41. }
  42. public void setMobileIp(String mobileIp) {
  43. this.mobileIp = mobileIp;
  44. }
  45. public int getMobilePort() {
  46. return mobilePort;
  47. }
  48. public void setMobilePort(int mobilePort) {
  49. this.mobilePort = mobilePort;
  50. }
  51. public int getComeFrom() {
  52. return comeFrom;
  53. }
  54. public void setComeFrom(int comeFrom) {
  55. this.comeFrom = comeFrom;
  56. }
  57. public String getBsid() {
  58. return bsid;
  59. }
  60. public void setBsid(String bsid) {
  61. this.bsid = bsid;
  62. }
  63. public String getUsername() {
  64. return username;
  65. }
  66. public void setUsername(String username) {
  67. this.username = username;
  68. }
  69. private String url;
  70. private String visitTime;
  71. private String onlineTime;
  72. private String desIp;
  73. private int desPort;
  74. private String mobileIp;
  75. private int mobilePort;
  76. private int comeFrom;
  77. private String bsid;
  78. private String username;
  79. private int fileUrlId;
  80. private int houseCityId;
  81. public int getHouseCityId() {
  82. return houseCityId;
  83. }
  84. public void setHouseCityId(int houseCityId) {
  85. this.houseCityId = houseCityId;
  86. }
  87. public int getFileUrlId() {
  88. return fileUrlId;
  89. }
  90. public void setFileUrlId(int fileUrlId) {
  91. this.fileUrlId = fileUrlId;
  92. }
  93.  
  94. }

获取配置文件类:

  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. /**
  6. * 读取properties文件
  7. *
  8. */
  9. public class Configuration
  10. {
  11. private Properties propertie;
  12. private InputStream in;
  13.  
  14. /** *//**
  15. * 初始化Configuration类
  16. */
  17. public Configuration()
  18. {
  19. propertie = new Properties();
  20. }
  21.  
  22. /** *//**
  23. * 初始化Configuration类
  24. * @param filePath 要读取的配置文件的路径+名称
  25. */
  26. public Configuration(String filePath)
  27. {
  28. propertie = new Properties();
  29. try{
  30. in =Object.class.getResourceAsStream(filePath);
  31. propertie.load(in);
  32. in.close();
  33. } catch (FileNotFoundException ex){
  34. System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
  35. ex.printStackTrace();
  36. } catch (IOException ex){
  37. System.out.println("装载文件--->失败!");
  38. ex.printStackTrace();
  39. }
  40. }//end ReadConfigInfo(...)
  41.  
  42. /** *//**
  43. * 重载函数,得到key的值
  44. * @param key 取得其值的键
  45. * @return key的值
  46. */
  47. public String getValue(String key)
  48. {
  49. if(propertie.containsKey(key)){
  50. String value = propertie.getProperty(key);//得到某一属性的值
  51. return value;
  52. }
  53. else
  54. return "";
  55. }//end getValue(...)
  56.  
  57. /** *//**
  58. * 重载函数,得到key的值
  59. * @param fileName properties文件的路径+文件名
  60. * @param key 取得其值的键
  61. * @return key的值
  62. */
  63. public String getValue(String fileName, String key)
  64. {
  65. try{
  66. String value = "";
  67. in = Object.class.getResourceAsStream(fileName);
  68. propertie.load(in);
  69. in.close();
  70. if(propertie.containsKey(key)){
  71. value = propertie.getProperty(key);
  72. return value;
  73. }else
  74. return value;
  75. } catch (FileNotFoundException e){
  76. e.printStackTrace();
  77. return "";
  78. } catch (IOException e){
  79. e.printStackTrace();
  80. return "";
  81. } catch (Exception ex){
  82. ex.printStackTrace();
  83. return "";
  84. }
  85. }//end getValue(...)
  86. }

oraclehelper.jar

ojdbc6

java读取文件批量插入记录的更多相关文章

  1. Java实现http大文件流读取并批量插入数据库

    1.概述 请求远程大文本,使用流的方式进行返回.需要设置http链接的超时时间 循环插入到List中,使用mybatis-plus批量插入到mysql中 2.需求 两台服务器 大文件放到其中一台服务器 ...

  2. [Java]读取文件方法大全(转)

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

  3. Java 读取文件的内容

    Java 读取文件的内容 1) CLASS_NAME: 换成自己真实的类名 2) /page/test.json: 换成自己真实的page 3) FileUtils: 来自于org.apache.co ...

  4. Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别

    一.Java读取和存储文件数据流 Java读取文件,实际是将文件中的字节流转换成字符流输出到屏幕的过程   这里面涉及到两个类:InputStreamReader和OutputStreamWriter ...

  5. Java使用iBatis批量插入数据到Oracle数据库

    Java使用iBatis批量插入数据到Oracle数据库 因为我们的数据跨库(mysql,oracle),单独取数据的话需要遍历好多遍,所以就想着先从mysql数据库中取出来的数据然后在oracle数 ...

  6. java IO 文件批量重命名

    java IO 文件批量重命名 package com.vfsd.renamefile; import java.io.File; import java.io.FileInputStream; im ...

  7. python 读取本地文件批量插入mysql

    Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考 133584752 133584759 133584764 133584773 133584775 13358477 ...

  8. Java读取文件的几种方式

    package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  9. java实现文件批量导入导出实例(兼容xls,xlsx)

    1.介绍 java实现文件的导入导出数据库,目前在大部分系统中是比较常见的功能了,今天写个小demo来理解其原理,没接触过的同学也可以看看参考下. 目前我所接触过的导入导出技术主要有POI和iRepo ...

随机推荐

  1. MySQL数据库中字符集的问题

    今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...

  2. 通过重写OnScrollListener来监听RecyclerView是否滑动到底部

    为了增加复用性和灵活性,我们还是定义一个接口来做监听滚动到底部的回调,这样你就可以把它用在listview,scrollView中去. OnBottomListener package kale.co ...

  3. dp和px以及sp

    dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...

  4. IOS 计步器

    这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变. 但是我会对之前之后的都进行简单介绍. IOS 8 - // // ViewController.m ...

  5. iOS网络检测Reachability 使用 Demo,可检测2、3、4G

    你可以在Github下载这个Demo https://github.com/JanzTam/Reachability_Demo 首先,引入系统的Reachability类,不知道怎么引入的话,在Xco ...

  6. Mac 下使用sourcetree操作git教程

    SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交.push.pu ...

  7. linux64位操作系统装32位jdk解决方法

    /opt/tomcat/tomcat7.0/bin/catalina.sh: /usr/local/java/jdk1.7.0_79/bin/java: /lib/ld-linux.so.2: bad ...

  8. Git+GitHub 使用小结

    1.Git安装完成后需要做的配置            $ git config --global user.name "Your Name"        $ git confi ...

  9. [windows]利用IPSec对指定的ip进行访问限制

    以win2003系统为例: 操作(看图): 1.任务:现在192.168.2.200可访问;目的;本地禁止对其访问 2.进入:管理工具->本地安全设置->IP安全策略 3.右键创建IP安全 ...

  10. 使用backbone.js、zepto.js和trigger.io开发HTML5 App

    为了力求运行速度快.响应迅即,我们推荐使用backbone.js和zepto.js. 为了让这个过程更有意思,我们开发了一个小小的示例项目,使用CSS重置样式.Backbone.js和带转场效果的几个 ...