日期:2014-05-16  浏览次数:20539 次

java读取文本文件到mysql数据库【示例2】

1、java读取文本文件到mysql数据库【示例】:把手机号码归属地文件:安徽联通.txt读取到数据库school的phonenumber表中

 

2、安徽联通.txt内容如下:

13003000000-13003009999-合肥
13003010000-13003029999-蚌埠
13003030000-13003049999-芜湖
13003050000-13003069999-合肥
13003070000-13003079999-淮南
13003080000-13003089999-合肥
13003090000-13003099999-巢湖
13004000000-13004009999-淮南
13004010000-13004039999-阜阳
13004040000-13004069999-芜湖
13004070000-13004099999-蚌埠
13010300000-13010309999-合肥
13013000000-13013029999-滁州
13013030000-13013039999-巢湖
13013040000-13013049999-池州
13013050000-13013059999-淮南
13013060000-13013099999-合肥
13013100000-13013119999-马鞍山
13013120000-13013129999-黄山
13013130000-13013149999-宣城
13013150000-13013159999-铜陵
13013160000-13013199999-安庆
13014000000-13014019999-宿州
13014020000-13014039999-六安
13014040000-13014059999-淮北
13014060000-13014099999-阜阳
13023000000-13023009999-合肥
13023010000-13023029999-蚌埠
13023030000-13023049999-芜湖
13023050000-13023069999-合肥
13023070000-13023079999-淮南
13023080000-13023089999-合肥
13023090000-13023099999-巢湖
13024000000-13024009999-淮南
13024010000-13024039999-阜阳
13024040000-13024069999-芜湖
13024070000-13024099999-蚌埠
13026000000-13026019999-安庆
13026020000-13026039999-滁州


 

3、java读取该文本文件:源码如下

package com.insigma.zd.group4.liuchao.jdbc;

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.SQLException;
import java.sql.Statement;

public class ReadConfigureFile2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/test";
		String username = "root";
		String password = "1234";
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
			stmt = conn.createStatement();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		File file = new File("D:\\share\\手机号码归属地\\安徽联通.txt");
		FileInputStream fis = null;
		try {
			 fis = new FileInputStream(file);
			InputStreamReader input = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(input);
			String line = null;
			String sql = null;
			String info[] = null;
			String path = file.getAbsolutePath();//得到选择文件的全路径
			String fileName = path.substring(path.lastIndexOf("\\")+1, path.lastIndexOf("."));//取得所选文件名
			String province = fileName.substring(0,fileName.length()-2);
			String cardType = fileName.substring(fileName.length()-2);
			try {
				while((line = br.readLine())!= null){
					info = line.split("-");
					sql = sql = "insert into telephone(startPhone,endPhone,city,province,cardType)values('"+ info[0] +"','"+info[1]+"','"+info[2]+"','"+province+"','"+cardType+"')";
					stmt.executeUpdate(sql);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}