public int insertCourse(Course course) { PreparedStatement insertStatement, selectStatement; Connection conn = ConnectionUtil.getOracleConnection(); int responseCode = 0; try { String selectSql = "SELECT COUNT(*) AS TOTAL FROM COURSE WHERE COURSECODE = ?"; selectStatement = conn.prepareStatement(selectSql); selectStatement.setString(1, course.getCode()); ResultSet rs = selectStatement.executeQuery(); int rowCount = -1; while (rs.next()) rowCount = rs.getInt("TOTAL"); if (conn != null && rowCount == 0) { String sql = "INSERT INTO COURSE (COURSEID, TITLE, COURSECODE, MAXAPPLICANTS) VALUES (COURSEID_SEQ.NEXTVAL, ?, ?, ?)"; insertStatement = conn.prepareStatement(sql); insertStatement.setString(1, course.getTitle()); insertStatement.setString(2, course.getCode()); insertStatement.setInt(3, course.getMaxApplicants()); responseCode = insertStatement.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } return responseCode; }
public int insertCourseInstance(CourseInstance instance) { PreparedStatement insertStatement; Connection conn = ConnectionUtil.getOracleConnection(); int responseCode = -1; try { String insertSql = "INSERT INTO COURSEINSTANCE (INSTANCEID, COURSEID, DURANCE, STARTDATE, PRICE) VALUES (INSTANCEID_SEQ.NEXTVAL, ?, ?, TO_DATE(?, 'dd-MM-yyyy'), ?)"; if (conn != null) { insertCourse(instance.getCourse()); Course course = getCourseByCode(instance.getCourse().getCode()); insertStatement = conn.prepareStatement(insertSql); insertStatement.setInt(1, course.getId()); insertStatement.setInt(2, instance.getDurance()); insertStatement.setString(3, DateUtil.toString(instance.getStartDate())); insertStatement.setDouble(4, instance.getPrice()); responseCode = insertStatement.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } return responseCode; }