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+"')");
stmt.executeUpdate(sql.toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
4、顯示已經成功讀取到數據庫中
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

