您的当前位置:首页正文

java使用load data 批量插入记录到数据库表中。

2024-11-28 来源:个人技术集锦
public class MysqlTest {

    static String driver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://10.119.8.67:3306/test_data?useSSL=false";
    static Connection conn = null;
    public static void main(String[] args){

        System.out.println("MysqlTest main start");

        try {
            Class.forName(driver);
            conn =  DriverManager.getConnection(url, "root", "Qwe@1234");
            String sql = "load data local infile \'E:\\\\work\\\\scala\\\\test\\\\cellNew0502.txt\' into table cell fields terminated by '|';";

            System.out.println("MysqlTest main sql " + sql );
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.execute();
            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("MysqlTest main end");
    }

load data local infile 'E:\\work\\scala\\test\\cellNew0502.txt' into table cell fields terminated by '|';

使用load data 插入数据效率非常快。




显示全文