日期:2014-05-16 浏览次数:20410 次
1、java读取文本文件到mysql数据库【示例】:把手机号码归属地文件:安徽联通.txt读取到数据库school的phonenumber表中
2、安徽联通.txt内容如下:
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-滁州 13026040000-13026059999-淮北 13026060000-13026069999-六安 13026070000-13026079999-宣城 13026080000-13026089999-马鞍山 13026090000-13026099999-宿州 13030600000-13030619999-淮北 13030620000-13030629999-池州 13030630000-13030639999-滁州 13030640000-13030699999-阜阳 13033000000-13033009999-合肥 13033010000-13033029999-蚌埠 13033030000-13033049999-芜湖 13033050000-13033069999-合肥 13033070000-13033079999-淮南 13033080000-13033089999-合肥 13033090000-13033099999-巢湖 13033100000-13033119999-马鞍山 13033120000-13033129999-黄山 13033130000-13033149999-宣城 13033150000-13033159999-铜陵 13033160000-13033199999-安庆 13034000000-13034019999-宿州 13034020000-13034039999-六安 13034040000-13034059999-合肥 13034060000-13034099999-阜阳 13035000000-13035009999-宿州 13035010000-13035029999-蚌埠 13035030000-13035039999-滁州 13035040000-13035049999-芜湖 13035050000-13035069999-合肥 13035070000-13035089999-阜阳 13035090000-13035099999-巢湖 13035400000-13035409999-合肥 13035410000-13035419999-阜阳 13035420000-13035429999-六安 13035430000-13035439999-黄山 13035440000-13035449999-马鞍山 13035450000-13035459999-滁州 13035460000-13035469999-淮北 13035470000-13035479999-淮南 13035480000-13035489999-铜陵 13035490000-13035499999-池州 13045500000-13045509999-滁州 13045510000-13045519999-合肥 13045520000-13045529999-六安
3、java读取该文本文件:源码如下
package com.insigma.zd.group4.liuchao.jdbc; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ReadConfigureFile { /** * @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/school"; String username = "root"; String password = "1234"; Connection conn = null; Statement stmt = null; File file = new File("D:\\share\\手机号码归属地\\安徽联通.txt"); StringBuffer sql = null; BufferedReader reader = null; String line = null; String[] str = null; String beginNumber = null; String endNumber = null; String city = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); reader = new BufferedReader(new FileReader(file)); stmt = conn.createStatement(); while((line = reader.readLine())!= null){ sql = new StringBuffer(); str = line.split("-"); beginNumber = str[0]; endNumber = str[1]; city = str[2]; sql.append("insert into phonenumber(beginNumber,endNumber,city) values('"); sql.append(beginNumber+"','"); sql.append(endNumber+"','"); sql.append(city+&quo