数组转成表映射对象
批量插入数据库
用java如何将txt文件导入mysqlJava中将txt文件导入到mysql基本的思路就是先使用I/O操作流获取到文件具体信息 , 然后将信息拼接成mysql插入到数据库中,示例如下:
1、先读取txt文件的内容,文件内容可以按照一定的规律进行排列,这样程序读取就方便 。
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.config.Constants;
import com.utils.UUIDUtil;
/**
*txt文本数据 采集类
【java导入文本文件代码 java怎么导入文本】*
* @see
*/
public class UserDataGather {
public static final StringTXT_FILE_PATH = "D://testUser.txt";
public static final String openFileStyle = "r";
public static final String fieldLimitChar = ".";
public static final int fieldAllCount = 1;
public static final String default_password = "PTMD0309";
public Integer count = 0;
private String FltNum;
public String UUID;
/**
* 功能:解析文本文件
*/
public void loadFile() {
try {
RandomAccessFile raf = new RandomAccessFile(TXT_FILE_PATH, openFileStyle);
String line_record = raf.readLine();
while (line_record != null) {
// 解析每一条记录
parseRecord(line_record);
line_record = raf.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 功能:具体解析每一条记录,这里可以增加很多对记录的解析判断条件,如是否为字母、
*/
@SuppressWarnings("static-access")
private void parseRecord(String line_record) throws Exception {
//拆分记录
//String[] fields = line_record.split(fieldLimitChar);
//System.out.println(tranStr(line_record)+"Ok");
String temp = line_record.substring(line_record.indexOf(fieldLimitChar, 0), line_record.indexOf(" ", line_record.indexOf(fieldLimitChar, 0)));
//if (fields.length == fieldAllCount) {
//
FltNum = tranStr(temp).trim().replace(fieldLimitChar,"").replace(" ","");
//System.out.println(FltNum);
if(FltNum.length()=4){
if(!isNumeric(FltNum)){
//System.out.println(generateSql(FltNum)[0].toString());
//System.out.println(generateSql(FltNum)[1].toString());
count++;
String[] temp1 = generateSql(FltNum);
MyFile mf = new MyFile();
mf.creatTxtFile("insertPinTuUserSql");
mf.writeTxtFile(temp1[0].toString());
mf.creatTxtFile("UUID");
mf.writeTxtFile(temp1[1].toString()+",");
}
}else if(FltNum.length() ==2 || FltNum.length() ==3){
if(!isNumeric(FltNum)){
if(!isTwoCharacter(FltNum)){
//System.out.println(generateSql(FltNum)[0].toString());
//System.out.println(generateSql(FltNum)[1].toString());
count++;
String[] temp2 = generateSql(FltNum);
MyFile mf = new MyFile();
mf.creatTxtFile("insertPinTuUserSql");
mf.writeTxtFile(temp2[0].toString());
mf.creatTxtFile("UUID");
mf.writeTxtFile(temp2[1].toString()+",");
}
}
}
//InsertDB db = new InsertDB();
//
//db.insertDB(FltNum);
//}
}
@SuppressWarnings("static-access")
public String[] generateSql(String userName) throws IOException{
StringBuffer sbf = new StringBuffer();
String[] str = new String[2];
String uuid = UUIDUtil.getUUID();
sbf.append("insert into user values('"+uuid+"','" + userName +"','"+default_password+"',"+Constants.ENABLED+","+Constants.NUllDELETE+","+Constants.AUDITING+",'"+uuid+"@164.com','"+formatDateTime()+"',"+Constants.REGEDIT_USER+");/n");
推荐阅读
- gis二三维地图,gis做三维地形图
- jquery怎么获父亲元素,jquery获取父元素方法
- js代码怎么登录,js登录界面代码编写
- 直播平台上买猫,直播卖猫注意什么
- java设计模式附代码 java设计模式实例
- jquery依赖注入,依赖注入 php
- microsoftnet安装失败,microsoftnetframework40怎么安装不了
- pes2013下载,pes2013下载地址
- python中函数导入 python 导入函数