日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2025/3/15 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;

總結

以上是生活随笔為你收集整理的jpa mysql脚本迁移_JPA通过LOAD DATA LOCAL INFILE大批量导入数据到MySQL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。