java读取文件批量插入记录
只是一个例子,方便以后查阅。
- import ey.db.oracle.OracleHelper;
- import ey.db.type.*;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- //import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URLDecoder;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.PreparedStatement;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- import oracle.jdbc.OracleTypes;
- public class ImpCNetData {
- static Configuration conf=new Configuration("/ProjectConfig.properties");
- static String db_host=conf.getValue("cdma_host");
- static String db_user=conf.getValue("cdma_user");
- static String db_passwd=conf.getValue("cdma_passwd");
- static String impFilePath=conf.getValue("ImpFilePath");
- static String impFileLog=conf.getValue("ImpFileLog");
- static String lastKeepString=conf.getValue("LastKeepString");
- static String lastFilterString=conf.getValue("LastFilterString");
- static String connStr="";
- static Pattern lastFilterRex=null;
- static Pattern lastKeepRex=null;
- private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
- //批量插入数据
- public void InsertDataBatch(List<CNetData> cnds) throws Exception
- {
- Connection conn=null;
- try {
- Class.forName(oracleDriverName);
- conn = DriverManager.getConnection(db_host, db_user, db_passwd);
- conn.setAutoCommit(false);
- 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,?,?,?,?,?,?,?,?,?,?,?,?,?)";
- PreparedStatement prest = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- for(int x = 0; x < cnds.size(); x++){
- CNetData cnd=cnds.get(x);
- prest.setString(1, cnd.getMobile());
- prest.setString(2, cnd.getUrl());
- prest.setString(3, cnd.getVisitTime());
- prest.setString(4, cnd.getOnlineTime());
- prest.setString(5,cnd.getDesIp());
- prest.setInt(6,cnd.getDesPort());
- prest.setString(7,cnd.getMobileIp());
- prest.setInt(8,cnd.getMobilePort());
- prest.setInt(9,cnd.getFileUrlId());
- prest.setInt(10,cnd.getComeFrom());
- prest.setString(11,cnd.getBsid());
- prest.setString(12,cnd.getUsername());
- prest.setInt(13,cnd.getHouseCityId());
- prest.addBatch();
- }
- prest.executeBatch();
- conn.commit();
- conn.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- if (conn!=null && !conn.isClosed())
- conn.close();
- }
- }
- //读取网址类型
- public DataTable GetUrlType()
- {
- try
- {
- OracleConnection conn= new OracleConnection(db_host+";"+db_user+";"+db_passwd);
- String cmd="FX114V01_CN_VISIT.GetUrlList";
- Parameter pds=new Parameter("out_data",OracleTypes.CURSOR,null, ParameterDirection.OUT);
- Parameter pcode=new Parameter("out_code",OracleTypes.INTEGER,null, ParameterDirection.OUT);
- DataSet ds=OracleHelper.ExecuteDataSet(conn,CommandType.StoreProcedure, cmd, pds,pcode);
- DataTable dt=ds.Tables[0];
- return dt;
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- return null;
- }
- //导入数据
- public void ImpData() throws Exception
- {
- //StringBuffer sb=new StringBuffer();
- String tempstr=null;
- String url="";
- DataTable dt=GetUrlType();//URL分类表
- if(lastFilterString.length()>0 && lastFilterRex==null)
- lastFilterRex=Pattern.compile(lastFilterString);
- if(lastKeepString.length()>0 && lastKeepRex==null)
- lastKeepRex=Pattern.compile(lastKeepString);
- try
- {
- //网址类型不能为空
- if(dt!=null)
- {
- //String path="/usr/hadoop/bigdata/filterurl/part/";
- File dir=new File(impFilePath);
- File[] files=null;
- if(dir.isDirectory())
- files=dir.listFiles(new MyFileFilter());
- for(int i=0;i<files.length;i++)
- {
- //System.out.println(files[i].getName());
- List<CNetData> cnds=new ArrayList<CNetData>();
- FileInputStream fis=new FileInputStream(files[i]);
- BufferedReader br=new BufferedReader(new InputStreamReader(fis,"UTF-8"));
- //文件逐行读取
- while((tempstr=br.readLine())!=null)
- {
- try
- {
- StringTokenizer itr=new StringTokenizer(tempstr,"|");
- if(itr.hasMoreTokens() && itr.countTokens()==11)
- {
- CNetData cnd=new CNetData();
- String mobile=itr.nextToken().trim();
- if(mobile.length()>11)
- cnd.setMobile(mobile.substring(0,11));
- else
- cnd.setMobile(mobile);
- url=itr.nextToken().trim();
- //替换URL特殊编码
- if(url.indexOf("%2F")>=0 || url.indexOf("%3F")>=0)
- {
- url = url.replaceAll("%2F","/");
- url = url.replaceAll("%3A",":");
- url = url.replaceAll("%20"," ");
- url = url.replaceAll("%3F","?");
- url = url.replaceAll("%3D","=");
- }
- //保留需要的
- if(lastKeepString.length()>0 && lastKeepRex!=null)
- {
- Matcher lastKeepMatcher=null;
- //防止引用或者搜索过来的
- if(url.length()<=30)
- lastKeepMatcher=lastKeepRex.matcher(url);
- else
- lastKeepMatcher=lastKeepRex.matcher(url.substring(0,30));
- //没找到的话跳出本次循环
- if(!lastKeepMatcher.find())
- continue; //跳出本次循环
- }
- //过滤不需要的
- if(lastFilterString.length()>0 && lastFilterRex!=null)
- {
- Matcher lastMatcher=lastFilterRex.matcher(url);
- //找到需要过滤掉的字符串
- if(lastMatcher.find())
- continue; //跳出本次循环
- }
- cnd.setUrl(url);
- cnd.setVisitTime(itr.nextToken());
- cnd.setMobileIp(itr.nextToken());
- cnd.setDesIp(itr.nextToken());
- cnd.setMobilePort(Integer.parseInt(itr.nextToken().trim()));
- cnd.setDesPort(Integer.parseInt(itr.nextToken().trim()));
- cnd.setOnlineTime(itr.nextToken());
- cnd.setBsid(itr.nextToken());
- cnd.setUsername(itr.nextToken());
- cnd.setComeFrom(Integer.parseInt(itr.nextToken().trim()));
- cnd.setFileUrlId(0);//默认值
- //查找所属URL类型
- for(int k=0;k<dt.Rows.length;k++)
- {
- if(url.indexOf(dt.Rows[k].Columns[1].colValue.toString())>=0)
- {
- cnd.setFileUrlId(Integer.parseInt(dt.Rows[k].Columns[0].colValue.toString().trim()));
- break;
- }
- }
- //判断房源或者链接的城市
- /*
- 21 北京
- 51 东莞
- 52 珠海
- 55 佛山
- 56 广州
- 59 惠州
- 68 深圳
- 162 武汉
- 174 长沙
- 341 上海
- * */
- if(url.indexOf("sz.")>=0||url.indexOf("sz/")>=0)
- cnd.setHouseCityId(68);
- else if(url.indexOf("gz.")>=0||url.indexOf("gz/")>=0||url.indexOf("gz_")>=0)
- cnd.setHouseCityId(56);
- else if(url.indexOf("dg.")>=0||url.indexOf("dg/")>=0||url.indexOf("dg_")>=0)
- cnd.setHouseCityId(51);
- else if(url.indexOf("fs.")>=0||url.indexOf("fs/")>=0||url.indexOf("fs_")>=0)
- cnd.setHouseCityId(55);
- else if(url.indexOf("zh.")>=0||url.indexOf("zh/")>=0||url.indexOf("zh_")>=0)
- cnd.setHouseCityId(52);
- else if(url.indexOf("hz.")>=0||url.indexOf("hz/")>=0||url.indexOf("hz_")>=0)
- cnd.setHouseCityId(59);
- else if(url.indexOf("zs.")>=0||url.indexOf("zs/")>=0||url.indexOf("zs_")>=0)
- cnd.setHouseCityId(73);
- else if(url.indexOf("bj.")>=0||url.indexOf("bj/")>=0||url.indexOf("bj_")>=0)
- cnd.setHouseCityId(21);
- else if(url.indexOf("sh.")>=0||url.indexOf("sh/")>=0||url.indexOf("sh_")>=0)
- cnd.setHouseCityId(341);
- else if(url.indexOf("cs.")>=0||url.indexOf("cs/")>=0||url.indexOf("cs_")>=0)
- cnd.setHouseCityId(174);
- else if(url.indexOf("wh.")>=0||url.indexOf("wh/")>=0||url.indexOf("wh_")>=0)
- cnd.setHouseCityId(162);
- else
- cnd.setHouseCityId(0);
- //上面的方式没找到的情况下才用下面的模式查找
- if(cnd.getHouseCityId()<=0)
- {
- if(url.contains("sz")||url.contains("shenzhen"))
- cnd.setHouseCityId(68);
- else if(url.contains("guangzhou"))
- cnd.setHouseCityId(56);
- else if(url.contains("dg")||url.contains("dongguan"))
- cnd.setHouseCityId(51);
- else if(url.contains("fs")||url.contains("foshan"))
- cnd.setHouseCityId(55);
- else if(url.contains("zhuhai"))
- cnd.setHouseCityId(52);
- else if(url.contains("hz")||url.contains("huizhou"))
- cnd.setHouseCityId(59);
- else if(url.indexOf("zs")>=0||url.indexOf("zhongshan")>=0)
- cnd.setHouseCityId(73);
- else if(url.indexOf("bj")>=0||url.indexOf("beijing")>=0)
- cnd.setHouseCityId(21);
- else if(url.indexOf("shanghai")>=0)
- cnd.setHouseCityId(341);
- else if(url.indexOf("cs")>=0||url.indexOf("changsha")>=0)
- cnd.setHouseCityId(174);
- else if(url.indexOf("wh")>=0||url.indexOf("wuhan")>=0)
- cnd.setHouseCityId(162);
- else
- cnd.setHouseCityId(0);
- }
- cnds.add(cnd);
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- //System.out.println(cnds.size());
- //插入数据
- if(cnds.size()>0)
- {
- InsertDataBatch(cnds);
- writeLog(files[i].getName()+" " +cnds.size());
- files[i].delete();
- }
- br.close();
- fis.close();
- //break;
- }
- }
- }
- catch(IOException ex)
- {
- System.out.println(ex.getStackTrace());
- }
- }
- public static void writeLog(String str)
- {
- try
- {
- //String path="/usr/hadoop/bigdata/filterurl/importfile.log";
- File file=new File(impFileLog);
- if(!file.exists())
- file.createNewFile();
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- FileOutputStream out=new FileOutputStream(file,true); //如果追加方式用true
- StringBuffer sb=new StringBuffer();
- //sb.append("-----------"+sdf.format(new Date())+"------------\n");
- sb.append(sdf.format(new Date())+" "+str+"\n");
- out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
- out.close();
- }
- catch(IOException ex)
- {
- System.out.println(ex.getStackTrace());
- }
- }
- public static void main(String[] args) throws Exception {
- try
- {
- ImpCNetData icd=new ImpCNetData();
- icd.ImpData();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
记录类:
- public class CNetData {
- private String mobile;
- public String getMobile() {
- return mobile;
- }
- public void setMobile(String mobile) {
- this.mobile = mobile;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getVisitTime() {
- return visitTime;
- }
- public void setVisitTime(String visitTime) {
- this.visitTime = visitTime;
- }
- public String getOnlineTime() {
- return onlineTime;
- }
- public void setOnlineTime(String onlineTime) {
- this.onlineTime = onlineTime;
- }
- public String getDesIp() {
- return desIp;
- }
- public void setDesIp(String desIp) {
- this.desIp = desIp;
- }
- public int getDesPort() {
- return desPort;
- }
- public void setDesPort(int desPort) {
- this.desPort = desPort;
- }
- public String getMobileIp() {
- return mobileIp;
- }
- public void setMobileIp(String mobileIp) {
- this.mobileIp = mobileIp;
- }
- public int getMobilePort() {
- return mobilePort;
- }
- public void setMobilePort(int mobilePort) {
- this.mobilePort = mobilePort;
- }
- public int getComeFrom() {
- return comeFrom;
- }
- public void setComeFrom(int comeFrom) {
- this.comeFrom = comeFrom;
- }
- public String getBsid() {
- return bsid;
- }
- public void setBsid(String bsid) {
- this.bsid = bsid;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- private String url;
- private String visitTime;
- private String onlineTime;
- private String desIp;
- private int desPort;
- private String mobileIp;
- private int mobilePort;
- private int comeFrom;
- private String bsid;
- private String username;
- private int fileUrlId;
- private int houseCityId;
- public int getHouseCityId() {
- return houseCityId;
- }
- public void setHouseCityId(int houseCityId) {
- this.houseCityId = houseCityId;
- }
- public int getFileUrlId() {
- return fileUrlId;
- }
- public void setFileUrlId(int fileUrlId) {
- this.fileUrlId = fileUrlId;
- }
- }
获取配置文件类:
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- /**
- * 读取properties文件
- *
- */
- public class Configuration
- {
- private Properties propertie;
- private InputStream in;
- /** *//**
- * 初始化Configuration类
- */
- public Configuration()
- {
- propertie = new Properties();
- }
- /** *//**
- * 初始化Configuration类
- * @param filePath 要读取的配置文件的路径+名称
- */
- public Configuration(String filePath)
- {
- propertie = new Properties();
- try{
- in =Object.class.getResourceAsStream(filePath);
- propertie.load(in);
- in.close();
- } catch (FileNotFoundException ex){
- System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
- ex.printStackTrace();
- } catch (IOException ex){
- System.out.println("装载文件--->失败!");
- ex.printStackTrace();
- }
- }//end ReadConfigInfo(...)
- /** *//**
- * 重载函数,得到key的值
- * @param key 取得其值的键
- * @return key的值
- */
- public String getValue(String key)
- {
- if(propertie.containsKey(key)){
- String value = propertie.getProperty(key);//得到某一属性的值
- return value;
- }
- else
- return "";
- }//end getValue(...)
- /** *//**
- * 重载函数,得到key的值
- * @param fileName properties文件的路径+文件名
- * @param key 取得其值的键
- * @return key的值
- */
- public String getValue(String fileName, String key)
- {
- try{
- String value = "";
- in = Object.class.getResourceAsStream(fileName);
- propertie.load(in);
- in.close();
- if(propertie.containsKey(key)){
- value = propertie.getProperty(key);
- return value;
- }else
- return value;
- } catch (FileNotFoundException e){
- e.printStackTrace();
- return "";
- } catch (IOException e){
- e.printStackTrace();
- return "";
- } catch (Exception ex){
- ex.printStackTrace();
- return "";
- }
- }//end getValue(...)
- }
java读取文件批量插入记录的更多相关文章
- Java实现http大文件流读取并批量插入数据库
1.概述 请求远程大文本,使用流的方式进行返回.需要设置http链接的超时时间 循环插入到List中,使用mybatis-plus批量插入到mysql中 2.需求 两台服务器 大文件放到其中一台服务器 ...
- [Java]读取文件方法大全(转)
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
- Java 读取文件的内容
Java 读取文件的内容 1) CLASS_NAME: 换成自己真实的类名 2) /page/test.json: 换成自己真实的page 3) FileUtils: 来自于org.apache.co ...
- Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别
一.Java读取和存储文件数据流 Java读取文件,实际是将文件中的字节流转换成字符流输出到屏幕的过程 这里面涉及到两个类:InputStreamReader和OutputStreamWriter ...
- Java使用iBatis批量插入数据到Oracle数据库
Java使用iBatis批量插入数据到Oracle数据库 因为我们的数据跨库(mysql,oracle),单独取数据的话需要遍历好多遍,所以就想着先从mysql数据库中取出来的数据然后在oracle数 ...
- java IO 文件批量重命名
java IO 文件批量重命名 package com.vfsd.renamefile; import java.io.File; import java.io.FileInputStream; im ...
- python 读取本地文件批量插入mysql
Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考 133584752 133584759 133584764 133584773 133584775 13358477 ...
- Java读取文件的几种方式
package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- java实现文件批量导入导出实例(兼容xls,xlsx)
1.介绍 java实现文件的导入导出数据库,目前在大部分系统中是比较常见的功能了,今天写个小demo来理解其原理,没接触过的同学也可以看看参考下. 目前我所接触过的导入导出技术主要有POI和iRepo ...
随机推荐
- MySQL数据库中字符集的问题
今天在做Hibernate案例,往mysql中写记录的时候,出现ERROR: Incorrect string value: '\xE5\x8A\xA0\xE5\x86\x85...' for col ...
- 通过重写OnScrollListener来监听RecyclerView是否滑动到底部
为了增加复用性和灵活性,我们还是定义一个接口来做监听滚动到底部的回调,这样你就可以把它用在listview,scrollView中去. OnBottomListener package kale.co ...
- dp和px以及sp
dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...
- IOS 计步器
这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变. 但是我会对之前之后的都进行简单介绍. IOS 8 - // // ViewController.m ...
- iOS网络检测Reachability 使用 Demo,可检测2、3、4G
你可以在Github下载这个Demo https://github.com/JanzTam/Reachability_Demo 首先,引入系统的Reachability类,不知道怎么引入的话,在Xco ...
- Mac 下使用sourcetree操作git教程
SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交.push.pu ...
- 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 ...
- Git+GitHub 使用小结
1.Git安装完成后需要做的配置 $ git config --global user.name "Your Name" $ git confi ...
- [windows]利用IPSec对指定的ip进行访问限制
以win2003系统为例: 操作(看图): 1.任务:现在192.168.2.200可访问;目的;本地禁止对其访问 2.进入:管理工具->本地安全设置->IP安全策略 3.右键创建IP安全 ...
- 使用backbone.js、zepto.js和trigger.io开发HTML5 App
为了力求运行速度快.响应迅即,我们推荐使用backbone.js和zepto.js. 为了让这个过程更有意思,我们开发了一个小小的示例项目,使用CSS重置样式.Backbone.js和带转场效果的几个 ...