1 year ago
#388848
stef_leggy
Parsing SQLite with JSON, Client to Server
I have got a GUI client, that can receive data from a multithreaded server. I then want to be able to send data back to the server. From what I understand with Json, I can use it to send the SQLite statement and also a string. I'm not sure if what I'm trying to implement is the best way forward, but this is what I have got. On the client:
private void addDriverToTable() {
if (printWriter != null && bufferedReader != null) {
String driverRef = driverRefTextField.getText();
String driverNumber = numberTextField.getText();
String driverCode = codeTextField.getText();
String driverForename = forenameTextField.getText();
String driverSurname = surnameTextField.getText();
String driverDOB = dobTextField.getText();
String driverNationality = nationalityTextField.getText();
String driverURL = urlTextField.getText();
System.out.println("get, " + driverRef + "," + driverNumber + "," + driverCode + "," + driverForename + ","
+ driverSurname + "," + driverDOB + "," + driverNationality + "," + driverURL);
String insertDriver = "INSERT INTO drivers (driverRef, number, code, forename, surname, dob, nationality, url)" +
"VALUES (?,?,?,?,?,?,?,?)";
String addDriver = (driverRef + "," + driverNumber + "," + driverCode + "," + driverForename + "," +
driverSurname + "," + driverDOB + "," + driverNationality + "," + driverURL);
addDriverHashMap = new HashMap();
addDriverHashMap.put(insertDriver, addDriver);
Gson gson = new Gson();
JsonObject addDriverData;
addDriverData = gson.toJsonTree(addDriverHashMap).getAsJsonObject();
System.out.println(addDriverData);
String toSend = "add";
printWriter.println(toSend);
String toSendJson = String.valueOf(addDriverData);
printWriter.println(toSendJson);
String reply = null;
printWriter.println(addDriverData);
statusLabel.setText("Status: Adding Driver to database");
try {
reply = bufferedReader.readLine();
statusLabel.setText("Status: Received reply from server");
}catch (IOException ex){
statusLabel.setText("IOException " + ex);
}
statusLabel.setText("Adding Driver to database");
}else {
statusLabel.setText("You must connect to the server first");
}
}
On the client handler:
@Override
public void run() {
try{
threadSays("Waiting for data from client...");
String lineRead;
while ((lineRead = bufferedReader.readLine()) != null){
threadSays("Read data from client: \"" + lineRead + "\".");
if(command.matches("add")){
if(command.matches("INSERT")) {
JsonObject jsonReply = JsonParser.parseString(arrayRead[0]).getAsJsonObject();
addMyDrivers = new ArrayList<>(Arrays.asList(jsonReply));
insertDriver = (String) addMyDrivers.get(1);
addDriver = (String) addMyDrivers.get(2);
replyMessage = "Adding Driver to Database";
ThreadedServer.addDriver(insertDriver, addDriver);
}
}
printWriter.println(replyMessage);
}
}catch (IOException ex){
Logger.getLogger(ClientHandlerThread.class.getName()).log(Level.SEVERE, null, ex);
}finally {
try {
threadSays("We have lost connection to client " + connectionNumber + ".");
ThreadedServer.removeThread(this);
socket.close();
}catch (IOException ex){
Logger.getLogger(ClientHandlerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Finally on the server:
public static void addDriver(String insertDriver, String addDriver) {
try (Connection connection = ConnectionSQLite.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(insertDriver)) {
preparedStatement.setString(1, addDriver);
preparedStatement.setString(2, addDriver);
preparedStatement.setString(3, addDriver);
preparedStatement.setString(4, addDriver);
preparedStatement.setString(5, addDriver);
preparedStatement.setString(6, addDriver);
preparedStatement.setString(7, addDriver);
preparedStatement.setString(8, addDriver);
preparedStatement.execute();
}
} catch (SQLException ex) {
Logger.getLogger(SQLException.class.getName()).log(Level.SEVERE, null, ex);
}
}
The SQLite message appears on the server, I can't work out how to actually get it to then do the insert, the data I'm trying to insert. Any help would be greatly appreciated.
java
json
sqlite
server
client
0 Answers
Your Answer