码迷,mamicode.com
首页 > 数据库 > 详细

数据导入 xls --》mysql

时间:2020-04-29 12:43:50      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:value   lombok   rownum   mysql   statement   sed   des   cut   cte   

1. 依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.6</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
        

2. 表数据对象

import lombok.Getter;

@Getter
public class Person {

    private int id;
    private String name;
    private int age;
    private String sex;
    private String company;
    private String year;
    private int salary;
}

3. listener

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.blb.espro.pojo.Person;
import com.blb.espro.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class PersonData extends AnalysisEventListener<Person> {

    private int max=10000;
    private List<Person> userList=new ArrayList<Person>();
    
    public void invoke(Person user, AnalysisContext analysisContext) {
        System.out.println(user);
        userList.add(user);
        if(userList.size()>=max)
        {
            DBUtil.batchSave(userList);
            userList.clear();
        }
    }
    
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        DBUtil.batchSave(userList);
    }
}

4. 数据导入

import com.alibaba.excel.EasyExcel;
import com.blb.espro.listener.PersonData;
import com.blb.espro.pojo.Person;
import lombok.Setter;
import java.sql.*;
import java.util.List;


public class DBUtil {



    private static String filePath = "C:\\Users\\Administrator\\Desktop\\222.xls";

    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("加载驱动成功!!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("加载驱动失败!!!");
        }
    }

    /*
?? ? * 连接数据的方法

?? ? */
    public static Connection getConn(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/es?useUnicode=true&characterEncoding=utf8", "root", "root");
            System.out.println("数据库连接成功!!");
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException("数据库连接失败!!");
        }
        return conn;
    }
    
    /*
?? ? * 关闭数据库连接
?? ? */
    public static void closeConn(Connection conn){
        try {
            if(conn != null){
                conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }


    public static  void batchSave(List<Person> userList)
    {
        Connection conn=getConn();
        String sql="insert into person values(?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            int i=1;
            for(Person person:userList)
            {
                preparedStatement.setInt(1,person.getId());
                preparedStatement.setString(2,person.getName());
                preparedStatement.setInt(3,person.getAge());
                preparedStatement.setString(4,person.getSex());
                preparedStatement.setString(5,person.getCompany());
                preparedStatement.setString(6,person.getYear());
                preparedStatement.setInt(7,person.getSalary());
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            preparedStatement.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        closeConn(conn);
    }


    public static void main(String[] args) {
        EasyExcel.read(filePath, Person.class, new PersonData()).sheet().headRowNumber(0).doRead();
    }
}

数据导入 xls --》mysql

标签:value   lombok   rownum   mysql   statement   sed   des   cut   cte   

原文地址:https://www.cnblogs.com/cjq10029/p/12801221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!