jpa mysql脚本迁移_JPA通过LOAD DATA LOCAL INFILE大批量导入数据到MySQL

连接层代码:

import org.hibernate.Session;

import org.hibernate.internal.SessionFactoryImpl;

import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public int loadFromInputStream(String loadDataSql, InputStream dataStream) throws JPAException {

Session session = (Session) entityManagerFactory.getDelegate();

SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();

int result = 0;

try {

//获取C3P0连接

Connection conn = sessionFactory.getConnectionProvider().getConnection();

PreparedStatement ps = conn.prepareStatement(loadDataSql);

//将C3P0的连接转换成mysql的

C3P0NativeJdbcExtractor cp30NativeJdbcExtractor = new C3P0NativeJdbcExtractor();

com.mysql.jdbc.Connection mysql_conn = (com.mysql.jdbc.Connection) cp30NativeJdbcExtractor.getNativeConnection(ps.getConnection());

//将C3P0的PreparedStatement转换成mysql的

com.mysql.jdbc.PreparedStatement mysql_ps = mysql_conn.prepareStatement(loadDataSql).unwrap(com.mysql.jdbc.PreparedStatement.class);

//设置文件流

mysql_ps.setLocalInfileInputStream(dataStream);

result = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

return result;

}

使用连接层代码:

public void testLoadFile() throws ServiceException {

String loadDataSql = "load data LOCAL infile 'C:/Users/Administrator/Desktop/userinfo.txt' into table userinfo character set utf8 fields terminated by ',' enclosed by '\"' lines terminated by '\r\n' (`userId`,`userName`,`userNickName`,`createTime`,`userEmail`,`userPassword`,`userSex`,`userMobile`,`lastLoginTime`);";

byte[] bytes = loadDataSql.getBytes();

InputStream is = new ByteArrayInputStream(bytes);

try {

userDAO.testLoadFile(loadDataSql, is);

} catch (JPAException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

userinfo.txt内容:

"2","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"3","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"4","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"5","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"6","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"7","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"8","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"9","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"10","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"11","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"12","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"13","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"14","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"15","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"16","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

"17","2","2","2017-01-13 14:03:53","2","2","2","2","2017-01-13 14:03:53"

userinfo表结构:

CREATE TABLE `userinfo` (

`userId` varchar(36) NOT NULL,

`userName` varchar(45) DEFAULT NULL,

`userNickName` varchar(45) DEFAULT NULL,

`createTime` datetime DEFAULT NULL,

`userEmail` varchar(45) DEFAULT NULL,

`userPassword` varchar(45) DEFAULT NULL,

`userSex` int(11) DEFAULT NULL,

`userMobile` varchar(45) DEFAULT NULL,

`lastLoginTime` datetime DEFAULT NULL,

PRIMARY KEY (`userId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值