IO流读取数据文件,将数据写入数据库,并记录数据导入日志

流程分析:

数据类型:

ROUTE_ID,LXBM,ROAD_NAME,SRC_LON,SRC_LAT,DEST_LON,DEST_LAT
10000,G50,沪渝高速,115.8605349,30.08934467,115.5437817,30.08898601
10001,G50,沪渝高速,115.5437817,30.08898601,115.2825297,30.28938191

需求分析:数据文件名就是数据库表名,数据类型大概就是第一行是字段名(但是里面的字段名不一定跟数据库中名字完全匹配,可能多了,可能少),第二行以及后面都是对应的数据。
需求设计:

  1. 第一步:找到文件存放的指定文件夹
  2. 第二步:循环读取这些文件,获取文件名并且去掉后缀
  3. 第三步:将文件名去掉_并且全部转为小写。
  4. 第四步:匹配对应的数据库
  5. 第五步:读取文件数据第一行,并用map存储字段名对应的位置index
  6. 第六步:继续读取下面的数据,并将数据通过逗号分隔,获取list,根据字段所在位置获取数据routeZonesTest.setDestLat(Double.valueOf(split.get(map.get("destlat"))));
  7. 第七步:将所有需要插入数据库的数据放在一个list中,只到一个文件中的数据读完。(这里采用批量插入效率会高很多)
  8. 第八步:将list数据插入对应的数据
  9. 第九步:将已经读取并且插入到数据库的文件移动到别的文件夹。
  10. 第十步:记录数据插入情况

下面是代码:

public Result<?> importDB() {
	List<String> filesPath = new ArrayList<String>();
	File files = new File(BaseConst.file_data_path);
	File[] tempList = files.listFiles();
	continueOut:
	for (int i = 0; i < tempList.length; i++) {

		// 如果目标文件 是文件
		int line = 1;
		String fileName = tempList[i].getName();
		if (tempList[i].isFile()) {
			System.out.println("文     件:" + tempList[i].getName().substring(0, tempList[i].getName().lastIndexOf(".")));
			filesPath.add(tempList[i].toString());
			File file = new File(tempList[i].toString());
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
				String tempString = null;
				Map<String, Integer> map = new HashMap<>();
				// 记录下每个字段对应的位置
				while ((tempString = reader.readLine()) != null) {
					List<String> split = Arrays.asList(tempString.split(","));
					if (line == 1) {
						System.out.println(tempString);
						for (int s = 0; s < split.size(); s++) {
							map.put(split.get(s).replace("_", "").replace(" ", "").toLowerCase(), s);
						}
					}
					break;
				}

				// 匹配该文件属于哪个数据库
				String fileNameNew = fileName.substring(0, tempList[i].getName().lastIndexOf(".")).replace("_", "")
						.replace(" ", "").toLowerCase();
				
				if (fileNameNew.equals("routezonestest")) {
					List<RouteZonesTest> routeZonesTestList = new ArrayList<>();
					while ((tempString = reader.readLine()) != null) {
						try {
							RouteZonesTest routeZonesTest = new RouteZonesTest();
							List<String> split = Arrays.asList(tempString.split(","));
							routeZonesTest.setDestLat(Double.valueOf(split.get(map.get("destlat"))));
							routeZonesTest.setDestLon(Double.valueOf(split.get(map.get("destlon"))));
							routeZonesTest.setRoadName(split.get(map.get("roadname")));
							if (map.get("routeid") == null) {
								dataImportLog(0, 0, "Missing primary key columns", fileName);
								continue continueOut;
							}
							if (split.get(map.get("routeid")) == null) {
								dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName);
								continue continueOut;
							}
							routeZonesTest.setRouteid(Integer.valueOf((split.get(map.get("routeid")))));
							routeZonesTest.setSrcLat(Double.valueOf(split.get(map.get("srclat"))));
							routeZonesTest.setSrcLon(Double.valueOf(split.get(map.get("srclon"))));
							routeZonesTestList.add(routeZonesTest);
							line++;
						} catch (Exception e) {
							dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); 
							continue continueOut;
						}
						
					}
					// 批量插入数据库
					try {
						routezonestest.insertDataBatch(routeZonesTestList);
					} catch (Exception e) { 
						dataImportLog(0, line+1, "Data insertion failed. ", fileName);
						continue;
					}
					
				} else if (fileNameNew.equals("routezonestest2")) {
					List<RouteZonesTest2> routeZonesTest2List = new ArrayList<>();
					while ((tempString = reader.readLine()) != null) {
						try {
							RouteZonesTest2 routeZonesTest2 = new RouteZonesTest2();
							List<String> split = Arrays.asList(tempString.split(",")); 
							routeZonesTest2.setDestLat(Double.valueOf(split.get(map.get("destlat")))); 
							routeZonesTest2.setDestLon(Double.valueOf(split.get(map.get("destlon"))));
							routeZonesTest2.setRoadName(split.get(map.get("roadname")));
							if (map.get("routeid") == null) {
								dataImportLog(0, 0, "Missing primary key columns", fileName);
								continue continueOut;
							}
							if (split.get(map.get("routeid")) == null) {
								dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName);
								continue continueOut;
							}
							routeZonesTest2.setRouteid(Integer.valueOf((split.get(map.get("routeid")))));
							routeZonesTest2.setSrcLat(Double.valueOf(split.get(map.get("srclat")))); 
							routeZonesTest2.setSrcLon(Double.valueOf(split.get(map.get("srclon"))));
							routeZonesTest2List.add(routeZonesTest2);
							line++;
						} catch (ArrayIndexOutOfBoundsException e) { 
							dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); 
							continue continueOut;
						}
					}
					reader.close();
					// 批量插入数据库
					try {
						routezonestest2.insertDataBatch(routeZonesTest2List);
					} catch (Exception e) {
						dataImportLog(0, line, "Data insertion failed. ", fileName);
						continue;
					}
				} else {// 没有找到对应的数据库
					dataImportLog(0, line, "The filename does not correspond to the database name.", fileName);
					continue;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			// 移动文件
			Result<?> removeFile = RemoveFile(file, BaseConst.file_data_used_path);
			if (removeFile.getCode() != 200) {
				dataImportLog(0, line, removeFile.getMsg(), fileName);
			} else {
				dataImportLog(1, line, null, fileName); 
			}
		}

		
		// 如果目标文件是文件夹 
		if (tempList[i].isDirectory()) {
			try {
				dataImportLog(0, 0, "The target is a folder, not a file", fileName);
			} catch (Exception e) {
				dataImportLog(0, 0, "The target is a folder, not a file, and the mobile file fails.", fileName);
			}
		}
		
	}

	return Result.returnResult();
}

private void dataImportLog(int status, int line, String reason, String fileName) {
	ImportDataLog importDataLog = new ImportDataLog();
	importDataLog.setStatus(0);
	importDataLog.setDataNumber(line);
	importDataLog.setReason(reason);
	importDataLog.setId(UUIDUtil.getUUID22());
	importDataLog.setFileName(fileName);
	importDataLog.setFileUsedName(fileName + "_" + DateUtil.formatDateTime(new Date()));
	importDataLog.setCreateTime(new Date());
	importDataLogMapper.insertSelective(importDataLog);

}

private Result<?> RemoveFile(File file, String destinationFloderUrl) {
	File destFloder = new File(destinationFloderUrl);
	// 检查目标路径是否合法
	if (destFloder.exists()) {
		if (destFloder.isFile()) {
			return Result.returnErrorResult("The target path is a file. Please check the target path!");
		}
	} else {
		if (!destFloder.mkdirs()) {
			return Result.returnErrorResult("Target folder does not exist, creation failed!");
		}
	}
	// 检查源文件是否合法
	if (file.isFile() && file.exists()) {
		String destinationFile = destinationFloderUrl + "\\" + file.getName(); 
		if (!file.renameTo(new File(destinationFile))) {
			return Result.returnErrorResult("Failed to move files!");
		}
	} else {
		return Result.returnErrorResult("The backup file path is incorrect, and the migration fails.");
	}
	return Result.returnResult();
}
	

日志记录:

由于插入数据肯定会因为数据存在问题,或者文件类型以及文件名存在问题而导致插入不成功,所以将这几种异常情况需要处理并且记录到日志文件中

  1. 情况1:数据文件中,数据库需要的主键列没有,则这个数据插入肯定失败,然后将插入日志写入数据库,文件不转移,并且继续执行下一个文件的数据插入if (map.get("routeid") == null) { dataImportLog(0, 0, "Missing primary key columns", fileName); continue continueOut; }
  2. 情况1:数据文件中,某一条数据没有数据,或者数据库不够,没有找到主键对应的数据,则这个数据插入失败,然后将插入日志写入数据库(同时记录在哪一行失败的),文件不转移,并且继续执行下一个文件的数据插入if (split.get(map.get("routeid")) == null) { dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName); continue continueOut; }
  3. 情况3:尽管在讲数据放入list时做了异常情况处理,但是还是多加一个catch来补货异常} catch (Exception e) { dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); continue continueOut; }
  4. 情况4:在将已经封装好的list数据插入数据库中也可能存在异常try { routezonestest.insertDataBatch(routeZonesTestList); } catch (Exception e) { dataImportLog(0, line+1, "Data insertion failed. ", fileName); continue; }
  5. 情况5:没有找到对应的数据库dataImportLog(0, line, "The filename does not correspond to the database name.", fileName); continue;
  6. 情况6:目标不是文件,而是文件夹dataImportLog(0, 0, "The target is a folder, not a file", fileName);
  7. 情况7::移动文件失败(失败的原因有很多,具体见RemoveFile方法) dataImportLog(0, line, removeFile.getMsg(), fileName);

批量插入sql

  <insert id="insertDataBatch" parameterType="java.util.List" >
 	 insert into ROUTE_ZONES_TEST2 (ROUTEID, ROAD_NAME, SRC_LAT, SRC_LON, DEST_LAT, DEST_LON )
    values 
    <foreach collection="list" item="bean" separator=",">
    	(#{bean.routeid},#{bean.roadName},#{bean.srcLat},#{bean.srcLon},#{bean.destLat},#{bean.destLon})
    </foreach>
  </insert>
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel POI读取封装(文件+示范代码) package org.excel.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import javax.jws.WebService; import org.apache.poi.hssf.usermodel.*; import org.excel.data.DataType; import org.excel.data.DealForeign; import org.excel.data.ExcelImport; import org.excel.data.impl.StInStatusImpl; import org.excel.data.impl.StuClassImportImpl; import org.excel.data.impl.StuEducationImpl; import org.excel.data.impl.StuImprotDataImpl; import org.excel.data.impl.StuInClassImportImpl; import org.excel.data.impl.StuWorkStatusImpl; import org.excel.manager.Util; import org.excel.model.ExcelMap; import org.excel.xml.ExcelXmlParse; import net.sourceforge.jtds.jdbcx.JtdsDataSource; @WebService public class ExcelServiceImpl implements IExcelService { String tableName = ""; public static Integer suc = 0; public static Integer fail = 0; StringBuilder insert = new StringBuilder( " insert into {tableName} {column} values {values}"); /** 读取Excel表格数据 */ public List<HashMap> readExcel(String excelName, Integer sheetIndex, String startPoint, String endPoint) throws Exception { FileInputStream inputStream = null; inputStream = new FileInputStream(excelName); HSSFWorkbook workbook = new HSSFWorkbook(inputStream); HSSFSheet sheet = workbook.getSheetAt(sheetIndex); String[] sc = null;// 开始坐标 String[] ec = null;// 结束坐标 int startRow = 0;// 默认开始行数 int endRoe = sheet.getPhysicalNumberOfRows();// 默认结束行 int startLine = 0;// 默认开始列 int endLine = 0;// 结束列 if (startPoint != null && endPoint != null) { sc = startPoint.
您可以使用Java的ZipInputStream类来读取zip压缩文件,然后将数据写入数据库。以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileReader { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String dbUrl = "jdbc:mysql://localhost:3306/mydatabase"; String dbUsername = "your-username"; String dbPassword = "your-password"; try { // 打开数据库连接 Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword); // 创建插入数据的SQL语句 String insertQuery = "INSERT INTO your_table_name (file_name, file_data) VALUES (?, ?)"; // 创建PreparedStatement对象 PreparedStatement pstmt = conn.prepareStatement(insertQuery); // 创建ZipInputStream对象 ZipInputStream zipInput = new ZipInputStream(new FileInputStream(zipFilePath)); // 读取zip文件中的每个条目 ZipEntry entry = zipInput.getNextEntry(); while (entry != null) { // 读取条目的文件名和数据 String fileName = entry.getName(); byte[] fileData = new byte[(int) entry.getSize()]; zipInput.read(fileData); // 设置参数并执行插入语句 pstmt.setString(1, fileName); pstmt.setBytes(2, fileData); pstmt.executeUpdate(); // 关闭当前条目,准备读取下一个条目 zipInput.closeEntry(); entry = zipInput.getNextEntry(); } // 关闭ZipInputStream、PreparedStatement和数据库连接 zipInput.close(); pstmt.close(); conn.close(); System.out.println("数据成功写入数据库!"); } catch (IOException | SQLException e) { e.printStackTrace(); } } } ``` 请将代码中的以下内容替换为您自己的信息: - `zipFilePath`:您的zip文件的路径。 - `dbUrl`:您的数据库URL。 - `dbUsername`:您的数据库用户名。 - `dbPassword`:您的数据库密码。 - `your_table_name`:您要插入数据的表名。 这段代码将逐个读取zip文件中的条目,并将每个条目的文件名和数据插入到数据库中。您需要根据自己的需求修改代码以适应您的数据库结构和数据处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值