/** * 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; }
/** * Fetch the sensor in the selected subArea. * * @param subAreaID * @param s */ public static void getSensorForSubArea(int subAreaID, Sensor s) { try { conn = DriverManager.getConnection( "jdbc:mysql://localhost/sosecurity?" + "user=ssuser&password=sspwd"); Statement stmt = null; ResultSet rs = null; try { stmt = (Statement) conn.createStatement(); String getSensor = "select * from sensor s where s.subAreaID=" + subAreaID; System.out.println(getSensor); try { rs = stmt.executeQuery(getSensor); try { while (rs.next()) { // rs = stmt.getResultSet(); int numColumns = rs.getMetaData().getColumnCount(); java.sql.ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= numColumns; i++) { System.out.println("Here !! " + rsmd.getColumnLabel(i) + " numcols:" + numColumns); s.setSensorID(rs.getInt("sensorID")); s.setFromTime(rs.getDate("start_time")); s.setToDate(rs.getDate("end_time")); s.setIsArmed(rs.getBoolean("isArmed")); } System.out.println(s.toString()); } } finally { try { rs.close(); } catch (Throwable ignore) { /* * Propagate the original exception instead of this * one that you may want just logged */ } } } finally { try { stmt.close(); } catch (Throwable ignore) { /* * Propagate the original exception instead of this one * that you may want just logged */ } } } 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 */ } } } 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; }