JPA通过LOAD DATA LOCAL INFILE大批量导入数据到MySQL
2017-01-14 00:00
225 查看
连接层代码:
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;
使用连接层代码:
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表结构:
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;
相关文章推荐
- 测试必备技能系列1 :通过mysql命令进行脚本数据导入
- asp.net excel数据通过程序导入mysql
- 通过Python将MongoDB导出的json数据转换成Mysql的insert语句导入
- 通过EXCEL将access数据库的数据导入到mysql
- mysql通过phpmyadmin导入excel表格的数据
- 通过Navicat向MySQL导入Excel文件格式数据的方法及遇到的问题
- mysql 通过Python导入clickhouse 行数对于完成速度的影响(数据文件无压缩)
- 如何通过PHP将excel的数据导入MySQL中
- mysql通过sql文件导入数据时出现乱码的解决办法
- 测试必备技能系列1 :通过mysql命令进行脚本数据导入
- 通过Navicat工具,将excel表格中的数据导入mysql
- 如何通过PHP将excel的数据导入MySQL中
- 如何通过PHP将excel的数据导入MySQL中
- MySQL通过命令导入导出数据
- Jpa生成mysql注释,添加ODBC数据源导入数据到EA
- MySQL通过命令导出导入数据和表
- 通过DIH工具增量将MySQL表中的数据导入Solr时,last_index_time小于当前时间8小时的问题
- 将HDFS数据通过Sqoop导入MySQL
- mysql通过sqoop导入到hbase中时数据量为1000w时出现Incorrect key file for table '/tmp/#sql_458_0.MYI'; try to repair it
- 如何通过PHP将excel的数据导入MySQL中