批量读取不同类型数据并存入不同数据库表

前言:读取trace和warning数据,批量存入不同的数据库表中
package com.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class write2Mysql {
	public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");  
        String databaseName = "dainci_1";// 已经在MySQL数据库中创建好的数据库。  
        String userName = "root";// MySQL默认的root账户名  
        String password = "1111";// 默认的root账户密码为空  
        	
	 
        String sql_trace = "INSERT INTO trace(eventType,traceInTime,collectorId,startFreq,stopFreq,rbw,refLevel,att,gain,pointNum,data)"
        		+ " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        String sql_warning = "INSERT INTO warning(eventType,warningInTime,collectorId,backgroundAmp,bandwidth,centerFreq,checkSignalBase,hazardLevel,modulationModel,spectrumAmp,time)"
        		+ " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName, userName, password);
//		记得设置为手动提交
		conn.setAutoCommit(false);
//		批量写入
		PreparedStatement	prest = conn.prepareStatement(sql_trace,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
		PreparedStatement	prest2 = conn.prepareStatement(sql_warning,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
		
		BufferedReader reader = new BufferedReader(new FileReader(new File("C:/Users/l.c/Desktop/tasks/electromagnetism/dianciFile.txt")));             		        		        		        
	    String line = null;
	    while ((line = reader.readLine()) != null) {			
//			转义符号			
			String[] tempString = line.split("\\|");
//			对数据类型进行判断
			if(tempString[0].equals("trace")){
				prest.setString(1, tempString[0]);  
		        prest.setString(2, tempString[1]);  
		        prest.setString(3, tempString[2]);  
		        prest.setString(4, tempString[3]);
		        prest.setString(5, tempString[4]);
		        prest.setString(6, tempString[5]);
		        prest.setString(7, tempString[6]);
		        prest.setString(8, tempString[7]);
		        prest.setString(9, tempString[8]);
		        prest.setString(10, tempString[9]);
		        prest.setString(11, tempString[10]);		        
		        prest.addBatch();		        
			}else if(tempString[0].equals("warning")){			
				prest2.setString(1, tempString[0]);  
		        prest2.setString(2, tempString[1]);  
		        prest2.setString(3, tempString[2]);  
		        prest2.setString(4, tempString[3]);
		        prest2.setString(5, tempString[4]);
		        prest2.setString(6, tempString[5]);
		        prest2.setString(7, tempString[6]);
		        prest2.setString(8, tempString[7]);
		        prest2.setString(9, tempString[8]);
		        prest2.setString(10, tempString[9]);
		        prest2.setString(11, tempString[10]);		        
		        prest2.addBatch();			    	
			}			        
		}		 
	    	prest2.executeBatch();
	    	prest.executeBatch();
	    	conn.commit();
	    	conn.close();
	    	prest.close(); 
	    	prest2.close();
		}
	}

发布了117 篇原创文章 · 获赞 8 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/u014257192/article/details/79097561