美文网首页Java学习之路
Java导入全国省市信息到数据库

Java导入全国省市信息到数据库

作者: 小鱼东西 | 来源:发表于2018-07-29 23:25 被阅读0次
将全国省市信息的json文本读出来并写入MySQL数据库中

json 部分数据格式如下(注意一个省的数据占一行)

[{"areacode":"100","city":"北京市","province":"北京"}]
[{"areacode":"210","city":"上海市","province":"上海"}]
[{"areacode":"220","city":"天津市","province":"天津"}]
[{"areacode":"230","city":"重庆市","province":"重庆"}]
[{"areacode":"280","city":"成都","province":"四川"},{"areacode":"812","city":"攀枝花","province":"四川"},{"areacode":"813","city":"自贡","province":"四川"},{"areacode":"816","city":"绵阳","province":"四川"},{"areacode":"817","city":"南充","province":"四川"},{"areacode":"818","city":"达州","province":"四川"},{"areacode":"825","city":"遂宁","province":"四川"},{"areacode":"826","city":"广安","province":"四川"},{"areacode":"827","city":"巴中","province":"四川"},{"areacode":"830","city":"泸州","province":"四川"},{"areacode":"831","city":"宜宾","province":"四川"},{"areacode":"832","city":"内江","province":"四川"},{"areacode":"833","city":"乐山","province":"四川"},{"areacode":"834","city":"凉山彝族自治州","province":"四川"},{"areacode":"835","city":"雅安","province":"四川"},{"areacode":"836","city":"甘孜藏族自治州","province":"四川"},{"areacode":"837","city":"阿坝藏族羌族自治州","province":"四川"},{"areacode":"838","city":"德阳","province":"四川"},{"areacode":"839","city":"广元","province":"四川"},{"areacode":"840","city":"资阳","province":"四川"},{"areacode":"841","city":"眉山","province":"四川"}]


package Main;

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.PreparedStatement;
import java.sql.SQLException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.sql.Statement;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class test {

    private static Statement stmt;
    private static Connection conn;
    private static StringBuffer sql;

    public static void main(String args[]) {
        String driver = "com.mysql.jdbc.Driver";     
        String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "123456";
        conn = null;
        stmt = null;

        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(new File("provices.txt")), "gbk"));
            String s = null;
            Class.forName(driver); 
            conn = DriverManager.getConnection(url, username, password);
            int id =1;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                JSONArray json = JSONArray.parseArray(s);
                stmt = conn.createStatement();
                for (int i = 0; i < json.size(); i++) {
                    
                    JSONObject ob = json.getJSONObject(i);
                    System.out.println(ob.get("areacode") + " " + ob.get("city") + " " + ob.get("province"));
                    sql = new StringBuffer();
                    sql.append("insert into provices(id,areacode,city,province) values("); 
                    sql.append(String.valueOf(id++)+",'");
                    sql.append(ob.get("areacode")+"','");    
                    sql.append(ob.get("city")+"','");   
                    sql.append(ob.get("province")+"')");
                    System.out.println(sql.toString());
                    
                    stmt.executeUpdate(sql.toString());
                }
//                  System.out.println(json);
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

数据库结果如下

image.png

相关文章

网友评论

    本文标题:Java导入全国省市信息到数据库

    本文链接:https://www.haomeiwen.com/subject/grwtvftx.html