mysql写入多个数据分隔符_mysql 分隔符(支持多个字符)批量导入

该博客介绍了如何使用Java和MySQL的LOAD DATA INFILE语句,通过自定义分隔符'@|@'批量导入数据到数据库表中。程序读取文本文件,每次500条数据提交一次事务,确保高效且稳定的导入过程。
摘要由CSDN通过智能技术生成

package com.nd.jdbc;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.util.List;

import org.apache.commons.io.FileUtils;

public class ImportMysqlTest {

/**

* @param args

*/

public static void main(String[] args) {

ImportMysqlTest sBase = new ImportMysqlTest();

sBase.ImportMysql("e://sina/out/sina_search.txt", "表名");

}

public void ImportMysql(String filepath, String tableName) {

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = BaseDao.getConn();

conn.setAutoCommit(false);

pstmt = conn

.prepareStatement("load data local infile '' "

+ "into table " + tableName

+ " fields terminated by '@|@'");

StringBuilder sb = new StringBuilder();

List str = FileUtils.readLines(new File(filepath), "UTF-8");

for (int i = 0; i < str.size(); i++) {

sb.append(str.get(i) + "\\n");

if (i % 500 == 0) {

InputStream is = new ByteArrayInputStream(sb.toString()

.getBytes("UTF-8"));

((com.mysql.jdbc.Statement) pstmt)

.setLocalInfileInputStream(is);

pstmt.execute();

conn.commit();

sb.setLength(0);

}

}

InputStream is = new ByteArrayInputStream(sb.toString().getBytes(

"UTF-8"));

((com.mysql.jdbc.Statement) pstmt).setLocalInfileInputStream(is);

pstmt.execute();

conn.commit();

} catch (Exception e) {

e.printStackTrace();

} finally {

BaseDao.closeAll(pstmt, conn);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值