jdbc批量執行SQL insert 操作


  • package com.file;  
  •   
  • import java.io.BufferedReader;  
  • import java.io.FileReader;  
  • import java.util.ArrayList;  
  •   
  • public class ResolvFile {  
  •     public static String readFileContent(String filepath) {  
  •         //1.讀取每一行記錄,保存到List中  
  •         ArrayList<String> records = new ArrayList<String>();  
  •         try {  
  •             BufferedReader br = new BufferedReader(new FileReader(filepath));  
  •               
  •             String aRecord;  
  •             while((aRecord = br.readLine())!=null){  
  •                 records.add(aRecord);//把讀取到的每一行記錄保存到List中  
  •             }  
  •             br.close();//用完以后關閉流  
  •         } catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •         //2.處理每一條記錄成SQL語句或保存為對象(a.去掉字段前后的分號b.拼接成SQL或者保存為對象)  
  •         ArrayList<String> recordList = new ArrayList<String>();//用於保存生成的SQL或對象  
  •         for(int i = 0;i<records.size();i++) {  
  •             String record = records.get(i);  
  •             String[] recArray = minusQuotation(record.split(","));  
  •               
  •             //拼接SQL語句或保存為對象  
  •             String recordSql = getRecordSql(recArray);  
  •             if (null!=recordSql) {  
  •                 recordList.add(recordSql);  
  •             }  
  •         }  
  •         //3.批量執行SQL或保存對象  
  •         batchExecuteSql(recordList);  
  •         return null;  
  •     }  
  •     public static int batchExecuteSql(ArrayList<String> sqlList) {  
  •         System.out.println("接下來可以執行SQL語句或保存對象");  
  •         System.out.println("========批量執行SQL語句==========");  
  •         System.out.println("將所有語句加入到Statment stat中");  
  •         for (int i = 0;i<sqlList.size();i++) {  
  •             String string = sqlList.get(i);  
  •             System.out.println("通過stat.addBatch(sql)來加入語句"+i+": '"+string+"'");  
  •         }  
  •         System.out.println("通過stat.executeBatch()來執行所有的SQL語句");  
  •         System.out.println("========批量執行SQL語句結束==========");  
  •         //int count = stat.executeBatch();  
  •         //return count;//返回執行的語句數量  
  •         return sqlList.size();  
  •     }  
  •     //生成每條記錄的SQL  
  •     public static String getRecordSql(String[] recArray) {  
  •         if (null==recArray) {  
  •             return null;  
  •         }  
  •         String recordSql = "insert into tablename (sms,no,time) values('"+recArray[0]+"','"+recArray[2]+"','"+recArray[5]+"')";  
  •         return recordSql;  
  •     }  
  •     /** 
  •      * 去掉數組中每一個元素的開頭和結尾的引號 
  •      * @param recArray 要處理的數組 
  •      * @return 處理后的數組 
  •      */  
  •     public static String[] minusQuotation(String[] recArray) {  
  •         for (int i = 0; i < recArray.length; i++) {  
  •             String str = recArray[i];  
  •             if (null!=str) {  
  •                 if(str.indexOf( "\"")==0)  
  •                     str = str.substring(1,str.length());//去掉開頭的分號  
  •                 if(str.lastIndexOf("\"")==(str.length()-1))  
  •                     str = str.substring(0,str.length()-1); //去掉最后的分號  
  •             }  
  •             recArray[i] = str;  
  •         }  
  •         return recArray;  
  •     }  
  •     public static void main(String[] args) {  
  •         String filepath = "E:\\sxySMS\\smstest.txt";  
  •         readFileContent(filepath);  
  •     }  

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM