/** * Method that edits a class from a database * * @author Ognjen Mišiæ * @param ca Class attendance we want to edit * @param classID we identify the class attendance by class id parameter * @throws ParseException * @throws SQLException */ public static void editClass(ClassAttendance ca, int classID) throws ParseException, SQLException { try { conn = connectToDb(); stmnt = createReadOnlyStatement(conn); rs = stmnt.executeQuery(SELECT_ALL_FROM_CLASSES_ATTENDANCE); String sql = "UPDATE classes_attendance SET class_description='" + ca.getClassDescription() + "', date='" + ca.getDate() + "', duration='" + ca.getDuration() + "' WHERE class_id='" + classID + "';"; while (rs.next()) { if (rs.getInt("class_id") == ca.getClassID()) { Statement stmnt2 = createUpdateableStatement(conn); if (ca.getClassDescription().length() < 2) { setErrorMessage("Invalid class description"); break; } else if (!isValidDate(ca.getDate())) { setErrorMessage("Invalid date."); break; } else if (ca.getDuration() < 1) { setErrorMessage("Invalid class duration"); break; } else { setErrorMessage(null); stmnt2.executeUpdate(sql); break; } } } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { rs.close(); } closeStatement(stmnt); closeConnection(conn); } }
/** * Method that takes a passed class attendance object and adds it to the table in db * * @author Ognjen Mišiæ * @param ca a new entry in to be added to the table */ public static void addClass(ClassAttendance ca) throws SQLException { if (ca != null) { try { conn = connectToDb(); stmnt = createUpdateableStatement(conn); String sql = "INSERT INTO classes_attendance (class_description, date, duration) VALUES('" + ca.getClassDescription() + "','" + ca.getDate() + "','" + ca.getDuration() + "');"; stmnt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { closeStatement(stmnt); closeConnection(conn); } } }