将数据从JSON文件插入MySql表

时间:2019-04-23 06:50:07

标签: java mysql json jdbc

我正在尝试将数据插入MySQL数据库表。 我已经这样创建了表:

mysql> SHOW COLUMNS FROM sampledata;
+------------------+---------------+------+-----+---------+-------+
| Field            | Type          | Null | Key | Default | Extra |
+------------------+---------------+------+-----+---------+-------+
| Confidence       | bigint(20)    | YES  |     | NULL    |       |
| ConnectionId     | bigint(20)    | YES  |     | NULL    |       |
| Imei             | bigint(20)    | YES  |     | NULL    |       |
| Imsi             | bigint(20)    | YES  |     | NULL    |       |
| IsData           | varchar(10)   | YES  |     | NULL    |       |
| IsSignalling     | varchar(10)   | YES  |     | NULL    |       |
| IsVoice          | varchar(10)   | YES  |     | NULL    |       |
| Latitude         | double(20,20) | YES  |     | NULL    |       |
| Longitude        | double(20,20) | YES  |     | NULL    |       |
| Mcc              | int(11)       | YES  |     | NULL    |       |
| Mnc              | int(11)       | YES  |     | NULL    |       |
| SegmentDuration  | time          | YES  |     | NULL    |       |
| SegmentStartTime | datetime      | YES  |     | NULL    |       |
| ServingCellLabel | varchar(20)   | YES  |     | NULL    |       |
| Sv               | int(11)       | YES  |     | NULL    |       |
| TrackingAreaCode | int(11)       | YES  |     | NULL    |       |
| Uncertainity     | int(11)       | YES  |     | NULL    |       |
+------------------+---------------+------+-----+---------+-------+
17 rows in set (0.00 sec)

我尝试了以下代码

package mysql;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.tools.JavaFileObject;

import jdk.nashorn.internal.parser.JSONParser;

public class sampleData {
    public int insertJSONtoDB() throws Exception {
    int status = 0;
    try {
        Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "user", "user123");
            PreparedStatement preparedStatement = con.prepareStatement("insert into  sampledata values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(new FileReader("/home/Desktop/PD/sampledata.json")); 
            JavaFileObject jsonObject = (JSONObject) obj;

            String id = (String) jsonObject.get("Confidence"); // from JSON tag
            preparedStatement.setString(1, Confidence); // to the Database table

            String name = (String) itemize.get("ConnectionId");
            preparedStatement.setString(2, ConnectionId);

            status = preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null) {
                    con.close();
                }

            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return status;
    }
}

但是它不起作用。另外,当我尝试导入任何SQL或JSON包时,它显示错误。我试图在Eclipse中设置构建路径,但无济于事。谁能告诉我我在做什么错。以下是我的JSON文件中的代码段。

{
        "Confidence": "1.994667E-07", 
        "Uncertainty": "178", 
        "IsData": "FALSE", 
        "Latitude": "1.694202", 
        "ConnectionId": "330708186825281", 
        "Mcc": "999", 
        "Sv": "01", 
        "Longitude": "0.434623", 
        "IsVoice": "FALSE", 
        "IsSignalling": "TRUE", 
        "SegmentStartTime": "16/02/2017 09:56:59.912", 
        "Imei": "99999006686069", 
        "SegmentDuration": "00:00:00.0350000", 
        "Mnc": "99", 
        "ServingCellLabel": "Cell18", 
        "Imsi": "999992223223602", 
        "TrackingAreaCode": "1234"
    }, 
    {
        "Confidence": "1.504506E-12", 
        "Uncertainty": "314", 
        "IsData": "FALSE", 
        "Latitude": "1.633704", 
        "ConnectionId": "260339442647675", 
        "Mcc": "999", 
        "Sv": "02", 
        "Longitude": "0.668554", 
        "IsVoice": "FALSE", 
        "IsSignalling": "TRUE", 
        "SegmentStartTime": "16/02/2017 09:57:01.377", 
        "Imei": "99999207564306", 
        "SegmentDuration": "00:00:00.0280000", 
        "Mnc": "99", 
        "ServingCellLabel": "Cell19", 
        "Imsi": "999993793410366", 
        "TrackingAreaCode": "1235"
    }, 
    {
        "Confidence": "0.3303348", 
        "Uncertainty": "129", 
        "IsData": "FALSE", 
        "Latitude": "1.847635", 
        "ConnectionId": "260339442647676", 
        "Mcc": "999", 
        "Sv": "14", 
        "Longitude": "1.356349", 
        "IsVoice": "FALSE", 
        "IsSignalling": "TRUE", 
        "SegmentStartTime": "16/02/2017 09:57:01.555", 
        "Imei": "99999605176135", 
        "SegmentDuration": "00:00:00.0290000", 
        "Mnc": "99", 
        "ServingCellLabel": "Cell13", 
        "Imsi": "999992216631694", 
        "TrackingAreaCode": "1236"
    }, 
    {
        "Confidence": "0.01800376", 
        "Uncertainty": "463", 
        "IsData": "FALSE", 
        "Latitude": "1.914598", 
        "ConnectionId": "330708186825331", 
        "Mcc": "999", 
        "Sv": "74", 
        "Longitude": "1.222736", 
        "IsVoice": "FALSE", 
        "IsSignalling": "TRUE", 
        "SegmentStartTime": "16/02/2017 09:57:02.689", 
        "Imei": "99999007880884", 
        "SegmentDuration": "00:00:00.0260000", 
        "Mnc": "99", 
        "ServingCellLabel": "Cell7", 
        "Imsi": "999992226681236", 
        "TrackingAreaCode": "1237"
    }

1 个答案:

答案 0 :(得分:0)

似乎您的文件中有数据数组,因此,您应该使Model对象能够读取。 免费使用Google提供的gson库 像

class CircleTransform : Transformation {
override fun transform(source: Bitmap?): Bitmap? {
    if (source == null) {
        return source
    }

    var bitmap: Bitmap

    // since we cant transform hardware bitmaps create a software copy first
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && source.config == Bitmap.Config.HARDWARE) {
        val softwareCopy = source.copy(Bitmap.Config.ARGB_8888, true)
        if (softwareCopy == null) {
            return source
        } else {
            bitmap = softwareCopy
            source.recycle()
        }
    } else {
        bitmap = source
    }

    var size = bitmap.width
    // if bitmap is non-square first create square one
    if (size != bitmap.height) {
        var sizeX = size
        var sizeY = bitmap.height
        size = Math.min(sizeY, sizeX)
        sizeX = (sizeX - size) / 2
        sizeY = (sizeY - size) / 2

        val squareSource = Bitmap.createBitmap(bitmap, sizeX, sizeY, size, size)
        bitmap.recycle()
        bitmap = squareSource
    }

    val circleBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(circleBitmap)
    val paint = Paint()
    val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)

    paint.shader = shader
    paint.isAntiAlias = true
    val centerAndRadius = size / 2f
    canvas.drawCircle(centerAndRadius, centerAndRadius, centerAndRadius, paint)

    bitmap.recycle()
    return circleBitmap
}


override fun key(): String {
    return "circleTransformation()"
}