zoukankan      html  css  js  c++  java
  • 读取文本写入数据库

    package dao;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class text4 {
    	/**
    	 * 读取文件
    	 * 
    	 * @param con
    	 *            数据库访问
    	 * @param csvFilePath
    	 *            文件路径
    	 */
    	private static Connection con;
    
    	public static void main(String[] args) throws SQLException, IOException {
    
    		long startTime = System.currentTimeMillis();
    		File file = new File(
    				"D:/BaiduNetdiskDownload/MY_DATA.LSHY__PX.txt");
    		
    		String encoding = "GBK";
    		InputStreamReader inputStreamReader = new InputStreamReader(
    				new FileInputStream(file), encoding);
    //				new FileInputStream(file));
    		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    		String line = null;
    		int count = 0;
    		int a = 0;
    //		String sql = "insert into outside_vehicle (HANGYE,GPYHANGYE,VEH_CODE,VEH_COLOR,INDUSTRY_TYPE,JIAMI_IDEN,WEIZHI,GPS_TIME,LONGITUDE,LATITUDE,GPS_SPEED,DBOARD_SPEED,LICHENG,DIRECTION,HAIBA,CAR_STATE,ALARM_STATE,LINKCODE)"
    //				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    		 String sql =
    		 "insert into jing_vehicle_seven (KEY_ZD,DATE_TYPE,VEH_CODE,VEH_COLOR,INDUSTRY_TYPE,GPS_TIME,LONGITUDE,LATITUDE,INSTANT_SPEED,DBOARD_SPEED,POSITIONING_STATE,FIRE_STATE,OVERSPEED_ALARM,EMERGENCY_ALARM,FATIGUE_DRIVING_ALARM,DIRECTION,ZLON,ZLAT,MATCHGRID,LINKCODE,LINKLEN,POINTTOLINE,LLON,LLAT,LONLAT5GEOKEY,LONLAT6GEOKEY,LONLAT7GEOKEY,LONLAT13GEOKEY,LONLAT14GEOKEY,LONLAT15GEOKEY,IDEN,AREA_HD)"
    		 +
    		 "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    		getConnect();
    		con.setAutoCommit(false);
    		PreparedStatement pStatement = con.prepareStatement(sql);
    		while ((line = bufferedReader.readLine()) != null) {
    			++a;
    			if (a == 1) {
    				continue;
    			}
    			String[] temp = line.split(",");
    			
    			if (temp.length <= 22)
    				continue;
    //			 if (temp.length == 23) {
    			if (temp.length == 32) {
    //
    //				 if(temp[2].contains("京")){
    //				 continue;
    //				 }
    //				 if(temp[7].contains("-")){
    //				 continue;
    //				 }
    //				 if(temp[8].contains("-")){
    //				 continue;
    //				 }
    				
    				
    //				pStatement.setString(1, temp[0]);
    //				pStatement.setString(2, temp[1]);
    //				pStatement.setString(3, temp[2]);
    //				pStatement.setDouble(4, Double.parseDouble(temp[3]));
    //				pStatement.setString(5, temp[4]);
    //				pStatement.setDouble(6, Double.parseDouble(temp[5]));
    //				pStatement.setString(7, temp[6]);
    //				pStatement.setDouble(8, Double.parseDouble(temp[7]));
    //				pStatement.setDouble(9, Double.parseDouble(temp[8]));
    //				pStatement.setDouble(10, Double.parseDouble(temp[9]));
    //				pStatement.setDouble(11, Double.parseDouble(temp[10]));
    //				pStatement.setDouble(12, Double.parseDouble(temp[11]));
    //				pStatement.setDouble(13, Double.parseDouble(temp[12]));
    //				pStatement.setDouble(14, Double.parseDouble(temp[13]));
    //				pStatement.setDouble(15, Double.parseDouble(temp[14]));
    //				pStatement.setDouble(16, Double.parseDouble(temp[15]));
    //				pStatement.setDouble(17, Double.parseDouble(temp[16]));
    //				pStatement.setDouble(18, Double.parseDouble(temp[17]));
    				
    
    				pStatement.setString(1, temp[0].trim());
    				pStatement.setString(2, temp[1].trim());
    				pStatement.setString(3, temp[2].trim());
    				pStatement.setString(4, temp[3].trim());
    				pStatement.setString(5, temp[4].trim());
    				pStatement.setString(6, temp[5].trim());
    				pStatement.setDouble(7, Double.parseDouble(temp[6].trim()));
    				pStatement.setDouble(8, Double.parseDouble(temp[7].trim()));
    
    				pStatement.setString(9, temp[8].trim());
    				pStatement.setString(10, temp[9].trim());
    				pStatement.setString(11, temp[10].trim());
    				pStatement.setString(12, temp[11].trim());
    				pStatement.setString(13, temp[12].trim());
    				pStatement.setString(14, temp[13].trim());
    				pStatement.setString(15, temp[14].trim());
    				pStatement.setString(16, temp[15].trim());
    				pStatement.setDouble(17, Double.parseDouble(temp[16].trim()));
    				pStatement.setDouble(18, Double.parseDouble(temp[17].trim()));
    				pStatement.setString(19, temp[18].trim());
    				pStatement.setString(20, temp[19].trim());
    				pStatement.setString(21, temp[20].trim());
    				pStatement.setString(22, temp[21].trim());
    				pStatement.setDouble(23, Double.parseDouble(temp[22].trim()));
    				pStatement.setDouble(24, Double.parseDouble(temp[23].trim()));
    
    				pStatement.setString(25, temp[24].trim());
    				pStatement.setString(26, temp[25].trim());
    				pStatement.setString(27, temp[26].trim());
    				pStatement.setString(28, temp[27].trim());
    				pStatement.setString(29, temp[28].trim());
    				pStatement.setString(30, temp[29].trim());
    				pStatement.setString(31, temp[30].trim());
    				pStatement.setString(32, temp[31].trim());
    
    				pStatement.addBatch();
    
    				count++;
    				System.out.println(count);
    //				System.out.println(temp[2]);
    				if (count == 200000) {
    					count = execute(pStatement, count);
    				} else if (count == 400000) {
    					count = execute(pStatement, count);
    				} else if (count == 600000) {
    					count = execute(pStatement, count);
    				} else if (count == 800000) {
    					count = execute(pStatement, count);
    				}
    			}
    		}
    		pStatement.executeBatch();
    		con.commit();
    		// Scanner in = new Scanner(file);
    		//
    		// getConnect();
    		// System.out.println("数据库连接成功");
    		// insert_data(in);
    
    		long EndTime = System.currentTimeMillis();
    		long time = (EndTime - startTime) / 1000;
    
    		System.out.println("导入数据共用时:" + time);
    	}
    
    	
    
    	
    	public static int execute(PreparedStatement pstmt, int count)
    			throws SQLException {
    		System.out.println("提交");
    		pstmt.executeBatch();
    		con.commit();
    		return 0;
    
    	}
    
    	private static void getConnect() {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			con = DriverManager.getConnection(
    					// "jdbc:mysql://192.168.8.157:3306/stopsix_two_phase",
    					// "vshop","fengyun2015");
    					"jdbc:mysql://localhost:3306/stopsix_two_phase", "root",
    					"root");
    
    		} catch (ClassNotFoundException | SQLException e) {
    			e.printStackTrace();
    		}
    	}
    
    }
  • 相关阅读:
    《当程序员的那些狗日日子》(六)继续熬夜学习的日子
    《当程序员的那些狗日日子》(四)喘过气来了
    《当程序员的那些狗日日子》(二)走上不归路
    《当程序员的那些狗日日子》(八)床上等你
    《当程序员的那些狗日日子》(一)毕业后的徘徊
    wince定时拍照功能(转)
    Excel公式找出某一列中是否有某值
    2010年到10月的流水帐
    不错的windows phone的博客
    将同一个表中的一个域更新到另外一个域的SQL文
  • 原文地址:https://www.cnblogs.com/liclBlog/p/15349564.html
Copyright © 2011-2022 走看看