【发布时间】:2019-09-12 07:34:47
【问题描述】:
我正在尝试将数据插入 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 文件中的 sn-p。
{
"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"
}
【问题讨论】:
-
“它不工作”是什么意思?错误?数据错误?没有数据?永远不要说“显示错误”。包括错误。他们不只是为了好玩,他们解释了问题所在。
-
您的 JSON 文件似乎是一个数组(尽管括号中没有显示 snipplet),所以您应该这样对待它并遍历它的元素。
-
@SamiKuhmonen 当我尝试运行该文件时,出现构建错误,例如包导入未完成。
-
@MauricePerry 是的,它是一个数组,sn-p 位于中间位置。