1. 程式人生 > >批量執行插入操作-幫同事做

批量執行插入操作-幫同事做

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