1. 程式人生 > >java讀取文字檔案到mysql資料庫【示例2】

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

}

4、顯示已經成功讀取到資料庫中: