zoukankan      html  css  js  c++  java
  • 批量执行插入操作-帮同事做

    package com.example.demo.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.PostConstruct;
    import java.io.*;
    
    /**
     * 执行大批量插入文本
     */
    @Service
    public class TxtServiceImpl implements TxtSevice{
    
        public  static int runningCount = 0; //正在执行的个数
        public  static int sucCount = 0; //成功个数
        public static int failCount = 0; //失败个数3
    
        @Value("${srcfile}")
        public String srcFile;
    
        @Value("${desfile}")
        public String desfile;
    
        @Value("${perprint}")
        public int perprint;
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
    
        @Override
        @PostConstruct//启动后自己执行
        public  void insertTxt() throws IOException {
    
                String name = srcFile;
                File file = new File(name);
                InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
                BufferedReader bf = new BufferedReader(inputReader);
                // 按行读取字符串
                String str = "";
                while ((str = bf.readLine()) != null) {
                    //str = new String(str.getBytes("gb2312"), "UTF-8");//将读取出来的GBK格式的代码转换成UTF-8
                    try{
                        jdbcTemplate.update(str);
                        sucCount++;
                        printRuningCount();
                    }catch (Exception e) {
                        writeFile(str);
                        failCount++;
                        printRuningCount();
                    }
                    //System.out.println(str);
                }
                bf.close();
                inputReader.close();
                System.out.println("执行总条数:"+(sucCount+failCount)+",  成功"+sucCount+"次  ,"+"失败"+failCount+"次  ,");
    
        }
    
    
        /**
         * 每xxx打印一次
         */
        private void printRuningCount(){
            runningCount++;
            if(runningCount%perprint==0){//每1000打印一次
                System.out.println("当前已经执行 "+runningCount+" 条");
            }
        }
    
        /**
         * 失败写文件
         * @param str
         * @throws IOException
         */
        public void writeFile(String str) throws IOException {
            File file = new File(desfile);
            FileOutputStream fos = null;
            OutputStreamWriter osw = null;
            BufferedWriter bw =null;
            try{
                if (!file.exists()) {
                    boolean hasFile = file.createNewFile();
                    if(hasFile){
                        //System.out.println("文件不存在,创建新文件");
                    }
                    fos = new FileOutputStream(file);
                } else {
                    //System.out.println("文件已经存在,正在进行追加数据");
                    fos = new FileOutputStream(file, true);
                }
                osw = new OutputStreamWriter(fos, "UTF-8");
                bw = new BufferedWriter(osw);
                bw.write(str);
                bw.newLine();//换行
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                bw.close();
                osw.close();
                fos.close();
            }
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <!-- plugins -->
    
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
    
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.29</version>
            </dependency>
    
        <!--    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>-->
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
        <repositories>
            <repository>
                <id>myRepository</id>
                <name>Repository for me</name>
                <url>http://192.168.100.10/nexus/content/groups/public/</url>
            </repository>
            <repository>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <id>central</id>
                <name>Central Repository</name>
                <url>http://repo.maven.apache.org/maven2</url>
            </repository>
        </repositories>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    
    
    </project>
    package com.example.demo;
    
    import com.example.demo.service.TxtSevice;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    import java.io.IOException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource
        TxtSevice txtService;
    
        @Test
        public void contextLoads() throws IOException {
    
            txtService.insertTxt();
        }
    
        @Test
        public void contextLoads1() throws IOException {
            System.out.println("吞吞吐吐");
        }
    
    
    }
    spring.datasource.url=xxx
    spring.datasource.username=xxx
    spring.datasource.password=xxx
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.max-idle=10
    spring.datasource.max-wait=10000
    spring.datasource.min-idle=5
    spring.datasource.initial-size=5
    
    srcfile=C:/All_Files/sql.txt
    desfile=C:/All_Files/failsql.txt
    perprint=1000

    -------------------------------------------

    1.帮同事写的脚本

    #!bin/sh
    for file in ./*
    do
        if test -f $file
        then
            echo $file 是文件
            suffix=.png
            if [[  $file == *$suffix  ]]; then
                 echo "我是png图片"
                #cp $file $storeDir
            fi
            suffix1=.jpg
            if [[  $file == *$suffix1  ]]; then
                 echo "我是jpg图片"
                #cp $file $storeDir
            fi
            #arr=(${arr[*]} $file)
        fi
        if test -d $file
        then
            echo $file 是目录
        fi
    done
  • 相关阅读:
    深入了解 JavaScript 中的 for 循环
    JavaScript 字符串常用操作
    10个习惯助你成为一名优秀的程序员
    Ubuntu日常问题搜集和解决办法
    提升你的开发效率,10 个 NPM 使用技巧
    Redux教程3:添加倒计时
    Redux教程2:链接React
    Redux教程1:环境搭建,初写Redux
    React + Redux 入坑指南
    12个非常实用的JavaScript小技巧
  • 原文地址:https://www.cnblogs.com/bee-home/p/10177458.html
Copyright © 2011-2022 走看看