读取文本写入数据库

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();
		}
	}

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TXTSQL是一种文本数据库,文件存储方式类似mysql,兼容部分sql语句.   PHP需4.0以上版本才能运行。   官方网站:http://txtsql.com/ txtSQL简单例子(来源:http://smvcn.xhblog.com/archives/2007/259845.shtml)   <?php   include('./txtSQL.class.php');   $sql = new txtSQL('./data');   $sql->connect('root', 'bar');   /*创建数据库   $sql->createdb(array('db' => 'demo')) or die('Error creating txtSQL DB, txtSQL said: '.$sql->get_last_error());   */   /*选择数据库*/   $sql->selectdb ( 'demo' );   /*创建表   $columns = array('id' => array('type' => 'int', 'auto_increment' => 1, 'permanent' => 1 ),   'name' => array('type' => 'string', 'max' => 50),   'email' => array('type' => 'string', 'max' => 50)   );   foreach($columns as $k => $v){   echo "\$colums[$k]=";   foreach($v as $k1 => $v1){   echo "$k1:$v1 ";   }   echo "<br>";   }   $sql->execute('create table',array(   'table' => 'table1',   'columns' => $columns)) or die('建表发生错误:'.$sql->get_last_error());   if($sql->table_exists('table1','demo')){   echo "建表成功";   }else{   echo "建表失败";   }   */   /*插入纪录   $value = array(0 => array('name' => 'abc','email' => 'abc@123.com'),   1 => array('name' => '123','email' => '123@bac.com'),   2 => array('name' => 'df','email' => 'reg@bac.com'),   3 => array('name' => 'trt','email' => 'hgh@bac.com'),   4 => array('name' => 'tyty','email' => 'ytyt@bac.com')   );   for($i=0;$i<5;$i++){   echo "\$value[$i]:";   foreach($value[$i] as $k => $v){   echo "[$k]=$v ";   }   echo "<br>";   }   for($i=0;$i<5;$i++){   if(!$sql->execute('insert',array('table' => 'table1','values' => $value[$i] ))){   die('An error occurred, txtSQL said: '.$sql->get_last_error());   }else{   echo "插入成功<br>";   }   }   */   /*修改表数据   $value = array('name' => 'abc','email' => 'aaa@123.com');   if(!$sql->execute('update',array('table' => 'table1','where' => array('id = 1'),'values' => $value))){   die('An error occurred, txtSQL said: '.$sql->get_last_error());   }else{   echo "修改成功";   }   */   /*删除表内容   $delete = array('table' => 'table1','where' => array('id > 1'));   $delcount = $sql->execute('delete',$delete);   echo $delcount;   */   /*显示表内容*/   $select = array('table' => 'table1','orderby' => array('id','asc')/*,'where' => array('email =~ %bac%'),'limit' => array(0,9)*/);   $data = $sql->execute('select',$select);   echo "<table border=1><tr><th>id</th><th>name</th><th>email</th></tr>";   foreach ( $data as $key => $row )   {   echo "<tr>";   foreach($row as $k => $v){   echo "<td>$v</td>";   }   echo "</tr>";   }   echo "</table>";   echo "表内共有 ".$sql->table_count('table1')." 行<BR>";   /*加入主键   $sql->execute('alter table',array('table' => 'table1','action' => 'addkey','name' => 'id','values' => array('name' => 'id')));   */   echo "最后插入ID号:".$sql->last_insert_id('table1') ;   $sql->disconnect();   ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值