jdbc批量执行SQL insert 操作

  1. package com.file;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.util.ArrayList;  
  6.   
  7. public class ResolvFile {  
  8.     public static String readFileContent(String filepath) {  
  9.         //1.读取每一行记录,保存到List中  
  10.         ArrayList<String> records = new ArrayList<String>();  
  11.         try {  
  12.             BufferedReader br = new BufferedReader(new FileReader(filepath));  
  13.               
  14.             String aRecord;  
  15.             while((aRecord = br.readLine())!=null){  
  16.                 records.add(aRecord);//把读取到的每一行记录保存到List中  
  17.             }  
  18.             br.close();//用完以后关闭流  
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.         //2.处理每一条记录成SQL语句或保存为对象(a.去掉字段前后的分号b.拼接成SQL或者保存为对象)  
  23.         ArrayList<String> recordList = new ArrayList<String>();//用于保存生成的SQL或对象  
  24.         for(int i = 0;i<records.size();i++) {  
  25.             String record = records.get(i);  
  26.             String[] recArray = minusQuotation(record.split(","));  
  27.               
  28.             //拼接SQL语句或保存为对象  
  29.             String recordSql = getRecordSql(recArray);  
  30.             if (null!=recordSql) {  
  31.                 recordList.add(recordSql);  
  32.             }  
  33.         }  
  34.         //3.批量执行SQL或保存对象  
  35.         batchExecuteSql(recordList);  
  36.         return null;  
  37.     }  
  38.     public static int batchExecuteSql(ArrayList<String> sqlList) {  
  39.         System.out.println("接下来可以执行SQL语句或保存对象");  
  40.         System.out.println("========批量执行SQL语句==========");  
  41.         System.out.println("将所有语句加入到Statment stat中");  
  42.         for (int i = 0;i<sqlList.size();i++) {  
  43.             String string = sqlList.get(i);  
  44.             System.out.println("通过stat.addBatch(sql)来加入语句"+i+": '"+string+"'");  
  45.         }  
  46.         System.out.println("通过stat.executeBatch()来执行所有的SQL语句");  
  47.         System.out.println("========批量执行SQL语句结束==========");  
  48.         //int count = stat.executeBatch();  
  49.         //return count;//返回执行的语句数量  
  50.         return sqlList.size();  
  51.     }  
  52.     //生成每条记录的SQL  
  53.     public static String getRecordSql(String[] recArray) {  
  54.         if (null==recArray) {  
  55.             return null;  
  56.         }  
  57.         String recordSql = "insert into tablename (sms,no,time) values('"+recArray[0]+"','"+recArray[2]+"','"+recArray[5]+"')";  
  58.         return recordSql;  
  59.     }  
  60.     /** 
  61.      * 去掉数组中每一个元素的开头和结尾的引号 
  62.      * @param recArray 要处理的数组 
  63.      * @return 处理后的数组 
  64.      */  
  65.     public static String[] minusQuotation(String[] recArray) {  
  66.         for (int i = 0; i < recArray.length; i++) {  
  67.             String str = recArray[i];  
  68.             if (null!=str) {  
  69.                 if(str.indexOf( "\"")==0)  
  70.                     str = str.substring(1,str.length());//去掉开头的分号  
  71.                 if(str.lastIndexOf("\"")==(str.length()-1))  
  72.                     str = str.substring(0,str.length()-1); //去掉最后的分号  
  73.             }  
  74.             recArray[i] = str;  
  75.         }  
  76.         return recArray;  
  77.     }  
  78.     public static void main(String[] args) {  
  79.         String filepath = "E:\\sxySMS\\smstest.txt";  
  80.         readFileContent(filepath);  
  81.     }  
posted @ 2016-06-20 17:31  Tim&Blog  阅读(4785)  评论(0编辑  收藏  举报