Java reads text files to the mysql database [Example 2]

Source: Internet
Author: User

1. java reads text files to the mysql database [Example]: reads the phonenumber table of the database school from the mobile phone number home location file: mongotong .txt.
 
The contents of the 2XX wildcard are as follows:
[Plain]
13003000000-13003009999-Hefei
130030316-13003029999-Bengbu
13003030000-13003049999-Wuhu
13003050000-13003069999-Hefei
13003070000-13003079999-Huainan
13003080000-13003089999-Hefei
13003090000-13003099999-Chaohu
13004000000-13004009999-Huainan
130040316-13004039999-Fuyang
13004040000-13004069999-Wuhu
13004070000-13004099999-Bengbu
13010300000-13010309999-Hefei
1301300316-13013029999-Chuzhou
13013030000-13013039999-Chaohu
13013040000-13013049999-Chizhou
13013050000-13013059999-Huainan
13013060000-13013099999-Hefei
13013100000-13013119999-ma'anshan
13013120000-13013129999-Huangshan
13013130000-13013149999-xuancheng
1301315316-13013159999-Tongling
1301316316-13013%99-Anqing
130141000000-1301409709-Suzhou
13014020000-13014039999-lu'an
13014040000-13014059999-Huaibei
13014060000-13014099999-Fuyang
13023000000-13023009999-Hefei
1302300000-13023029999-Bengbu
13023030000-13023049999-Wuhu
13023050000-13023069999-Hefei
13023070000-13023079999-Huainan
13023080000-13023089999-Hefei
13023090000-13023099999-Chaohu
13024000000-13024009999-Huainan
130240316-13024039999-Fuyang
13024040000-13024069999-Wuhu
13024070000-13024099999-Bengbu
13026000000-130260%9-Anqing
13026020000-13026039999-Chuzhou

 
3. Read the text file using java: the source code is as follows:
[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 \ mobile phone number attribution \ Unicom .txt ");
FileInputStream FCM = null;
Try {
FS = new FileInputStream (file );
InputStreamReader input = new InputStreamReader (FCM );
BufferedReader br = new BufferedReader (input );
String line = null;
String SQL = null;
String info [] = null;
String path = file. getAbsolutePath (); // obtain the full path of the selected file.
String fileName = path. substring (path. lastIndexOf ("\") + 1, path. lastIndexOf ("."); // get the selected file name
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 = "insert into telephone (startPhone, endPhone, city, province, cardType) values ('" + info [0] + "', '"+ info [1] +"', '"+ info [2] +"', '"+ province +"', '"+ cardType + "')";
Stmt.exe cuteUpdate (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. It indicates that the database has been read successfully:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.