-
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) {
-
- ArrayList<String> records = new ArrayList<String>();
- try {
- BufferedReader br = new BufferedReader(new FileReader(filepath));
-
- String aRecord;
- while((aRecord = br.readLine())!=null){
- records.add(aRecord);
- }
- br.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- ArrayList<String> recordList = new ArrayList<String>();
- for(int i = 0;i<records.size();i++) {
- String record = records.get(i);
- String[] recArray = minusQuotation(record.split(","));
-
-
- String recordSql = getRecordSql(recArray);
- if (null!=recordSql) {
- recordList.add(recordSql);
- }
- }
-
- 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语句结束==========");
-
-
- return sqlList.size();
- }
-
- 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;
- }
-
- 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);
- }
- }
-
相关阅读:
你的课程
asp.net的ajax以及json
asp.net自带的异步刷新控件使用
基于.net mvc的校友录(源程序)
基于.net mvc的校友录(七、文件上传以及多对多关系表的LINQ查询实现)
基于.net mvc的校友录(六、codefirst的使用以及班级模块的关键部分实现)
逻辑回归实例
层次聚类
k均值聚类
数据处理与转换
-
原文地址:https://www.cnblogs.com/wang3680/p/5601233.html
Copyright © 2020-2023
润新知