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 stubString 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 blocke1.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 blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(conn != null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}
4、显示已经成功读取到数据库中: