Я пытаюсь вставить данные в таблицу базы данных 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"
}