/** * Insert a new row into the table fireAlarm in database. * * @param fire */ public static void insertFireAlarm(FireAlarm fire) { System.out.println("Insert FireAlarm " + fire.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 = "insert into fireAlarm (subAreaID, userID, start_time, end_time, isArmed, active_date)" + "values (?,?,?,?,?,?)"; System.out.println(updateSubArea); PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement(updateSubArea); preparedStmt.setInt(1, fire.getSubAreaID()); preparedStmt.setLong(2, fire.getUserID()); preparedStmt.setDate(3, new java.sql.Date(fire.getFromTime().getTime())); if (fire.getToDate() == null) { preparedStmt.setDate(4, null); } else { preparedStmt.setDate(4, new java.sql.Date(fire.getToDate().getTime())); } preparedStmt.setBoolean(5, fire.getIsArmed()); preparedStmt.setDate(6, new java.sql.Date(fire.getActive_date().getTime())); // 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 */ } } } 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 fireAlarm object in the selected subArea * * @param subAreaID * @param f */ public static void getFireAlarmForSubArea(int subAreaID, FireAlarm f) { 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 fireAlarm s where s.subAreaID=" + subAreaID; System.out.println(getSensor); try { rs = stmt.executeQuery(getSensor); try { while (rs.next()) { 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); f.setFireAlarmID(rs.getInt("fireAlarmID")); f.setFromTime(rs.getDate("start_time")); f.setToDate(rs.getDate("end_time")); f.setIsArmed(rs.getBoolean("isArmed")); } System.out.println(f.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; }