/** * upDate the sensor with new start and end time and also enabled/disabled information in Database * * @param sensor */ public static void updateSensor(Sensor sensor) { System.out.println("Update sensor " + sensor.toString()); try { conn = DriverManager.getConnection( "jdbc:mysql://localhost/sosecurity?" + "user=ssuser&password=sspwd"); try { // String getSensor = "UPDATE sensor SET subAreaID=2, userID=1, // isArmed = true WHERE sensorID=1"; String updateSubArea = "UPDATE sensor SET subAreaID= ? , userID= ?, isArmed=?, start_time=?, end_time= ? " + "WHERE sensorID=?"; System.out.println(updateSubArea); PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement(updateSubArea); preparedStmt.setInt(1, sensor.getSubAreaID()); preparedStmt.setLong(2, sensor.getUserID()); preparedStmt.setBoolean(3, sensor.getIsArmed()); preparedStmt.setDate(4, new java.sql.Date(sensor.getFromTime().getTime())); if (sensor.getToDate() == null) { preparedStmt.setDate(5, null); } else { preparedStmt.setDate(5, new java.sql.Date(sensor.getToDate().getTime())); } preparedStmt.setInt(6, sensor.getSensorID()); // execute the java prepared statement int affectedRow = preparedStmt.executeUpdate(); System.out.println(affectedRow); } finally { // It's important to close the connection when you are done with // it try { conn.close(); } catch (Throwable ignore) { /* * Propagate the original exception instead of this one that * you may want just logged */ ignore.printStackTrace(); } } } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } return; }