Пример #1
0
 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;
 }