2012-04-24 2 views
0

사용자 입력 작업을하고 페이지 필터를 만들었습니다. 그것은 작동하지만, 나는 데이터베이스에 직접 쿼리를 이해합니다.복잡한 쿼리 기능 - 어떻게 작성합니까?

이 쿼리를 준비된 쿼리로 작동하도록 최적화 할 수 있습니까? 나는 if-else 문을 입력하기 전에 전문가와 확인하기로 결정했습니다. 당신을 도울 수있는

public ArrayList<data.exam> getAllExamsList(
     String course, String building, String room, 
     String capacity, String numberenrolled, String day, 
     String starttime, String endtime, String callnumber, 
     String department, String instructor, boolean coursebox, 
     boolean buildingbox, boolean roombox, boolean capacitybox, 
     boolean numberenrolledbox, boolean daybox, boolean startbox, 
     boolean endbox, boolean callbox, boolean departmentbox, 
     boolean instructorbox, String starttimesearchfrom, String starttimesearchto, 
     String endtimesearchfrom, String endtimesearchto) throws SQLException { 

    String findstarttimesearchfrom = starttimesearchfrom.equals("ALL") ? "" : " AND `Start Time`>=time('" + starttimesearchfrom + "')"; 
    String findstarttimesearchto = starttimesearchto.equals("ALL") ? "" : " AND `Start Time`<=time('" + starttimesearchto + "')"; 
    String findendtimesearchfrom = endtimesearchfrom.equals("ALL") ? "" : " AND `End Time`>=time('" + endtimesearchfrom + "')"; 
    String findendtimesearchto = endtimesearchto.equals("ALL") ? "" : " AND `End Time`<=time('" + endtimesearchto + "')"; 

    int totalOutPuts = 0; //how many checkboxes are checked 

    boolean checks[] = new boolean[]{coursebox, buildingbox, roombox, 
     capacitybox, numberenrolledbox, daybox, startbox, endbox, 
     callbox, departmentbox, instructorbox}; 

    for (boolean check : checks) { 
     if (check) { 
      totalOutPuts++; //adding to checked boxes count 
     } 
    } 

    String groupBy = " Group by "; //create a Group by statement for query 
    for (int i = 1; i < totalOutPuts; i++) { 
     groupBy += i + ", "; 
    } 
    groupBy += totalOutPuts + ""; //adding the last element to Group by w/o comma 

    StringWriter sw = new StringWriter(); 
    PrintWriter pw = new PrintWriter(sw); 
    Connection con = null; 
    Statement stmnt = null; 
    ResultSet displayString = null; 
    ArrayList<data.exam> exams = new ArrayList<data.exam>(); //will bre returned back with list of exams 

    String showcoursebox = coursebox == true ? "`Course Number`" : ""; 
    showcoursebox += (coursebox && (buildingbox || roombox || capacitybox || numberenrolledbox || daybox || startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showbuildingbox = buildingbox == true ? "Building" : ""; 
    showbuildingbox += (buildingbox && (roombox || capacitybox || numberenrolledbox || daybox || startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showroombox = roombox == true ? "`Room Number`" : ""; 
    showroombox += (roombox && (capacitybox || numberenrolledbox || daybox || startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showcapacitybox = capacitybox == true ? "`Room Capacity`" : ""; 
    showcapacitybox += (capacitybox && (numberenrolledbox || daybox || startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String shownumberenrolled = numberenrolledbox == true ? "`Number Enrolled`" : ""; 
    shownumberenrolled += (numberenrolledbox && (daybox || startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showdaybox = daybox == true ? "`Exam Day`" : ""; 
    showdaybox += (daybox && (startbox || endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showstartbox = startbox == true ? "`Start Time`" : ""; 
    showstartbox += (startbox && (endbox || callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showendbox = endbox == true ? "`End Time`" : ""; 
    showendbox += (endbox && (callbox || departmentbox || instructorbox)) ? ", " : ""; 

    String showcallbox = callbox == true ? "`Call Number`" : ""; 
    showcallbox += (callbox && (departmentbox || instructorbox)) ? ", " : ""; 

    String showdepartmentbox = departmentbox == true ? "Department" : ""; 
    showdepartmentbox += (departmentbox && (instructorbox)) ? ", " : ""; 

    String showinstructorbox = instructorbox == true ? "Instructor" : ""; 

    String findCourse = course.equals("ALL") ? "" : " AND `Course Number`=" + course; 
    String findBuilding = building.equals("ALL") ? "" : " AND `Building`='" + building + "'"; 
    String findRoom = room.equals("ALL") ? "" : " AND `Room Number`='" + room + "'"; 
    String findCapacity = capacity.equals("ALL") ? "" : " AND `Room Capacity`='" + capacity + "'"; 
    String findNumberEnrolled = numberenrolled.equals("ALL") ? "" : " AND `Number Enrolled`='" + numberenrolled + "'"; 
    String findDay = day.equals("ALL") ? "" : " AND `Exam Day` LIKE '%" + day.toLowerCase() + "%'"; 
    String findStarttime = starttime.equals("ALL") ? "" : " AND `Start Time`=time('" + starttime + "')"; 
    String findEndtime = endtime.equals("ALL") ? "" : " AND `End Time`=time('" + endtime + "')"; 
    String findCall = callnumber.equals("ALL") ? "" : " AND `Call Number`=" + callnumber; 
    String findDepartment = department.equals("ALL") ? "" : " AND `Department`='" + department + "'"; 
    String findInstructor = instructor.equals("ALL") ? "" : " AND `Instructor`='" + instructor + "'"; 

    String query = "select " 
      + showcoursebox 
      + showbuildingbox 
      + showroombox 
      + showcapacitybox 
      + shownumberenrolled 
      + showdaybox 
      + showstartbox 
      + showendbox 
      + showcallbox 
      + showdepartmentbox 
      + showinstructorbox 
      + " from" 
      + " (select " 
      + "  exam_schedules.course_id as id," 
      + "   rooms.number as 'Room Number'," 
      + "   rooms.building as Building," 
      + "   rooms.capacity as 'Room Capacity'," 
      + "   day as 'Exam Day'," 
      + "   start_time as 'Start Time'," 
      + "   end_time as 'End Time'" 
      + " from" 
      + "  exam_schedules, rooms" 
      + " where" 
      + "  exam_schedules.room_id = rooms.id) r1," 
      + " (select " 
      + "  courses.id," 
      + "   courses.number_enrolled as 'Number Enrolled'," 
      + "   courses.call_number as 'Call Number'," 
      + "   courses.course_number as 'Course Number'," 
      + "   departments.department_code as Department," 
      + "   instructors.full_name as Instructor" 
      + " from" 
      + "  courses, departments, instructors" 
      + " where" 
      + "  courses.department_id = departments.id and courses.instructor_id = instructors.id) r2" 
      + " where" 
      + " r2.id = r1.id " 
      + findCourse + findBuilding + findRoom + findCapacity 
      + findNumberEnrolled + findDay + findStarttime + findEndtime 
      + findCall + findDepartment + findInstructor 
      + findstarttimesearchfrom 
      + findstarttimesearchto 
      + findendtimesearchfrom 
      + findendtimesearchto 
      + groupBy; 

    try { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     con = (Connection) DriverManager.getConnection(DBurl, user, password); 
     stmnt = (Statement) con.createStatement(); 
     String readTopic = query; 
     displayString = stmnt.executeQuery(readTopic); 
     while (displayString.next()) { 
      try { 

       String courseObj = "", 
         buildingObj = "", 
         roomObj = "", 
         capacityObj = "", 
         numberenrolledObj = "", 
         dayObj = "", 
         starttimeObj = "", 
         endtimeObj = "", 
         callnumberObj = "", 
         departmentObj = "", 
         instructorObj = ""; 

       courseObj = coursebox == true ? displayString.getString("Course Number") : ""; 
       buildingObj = buildingbox == true ? displayString.getString("Building") : ""; 
       roomObj = roombox == true ? displayString.getString("Room Number") : ""; 
       capacityObj = capacitybox == true ? displayString.getString("Room Capacity") : ""; 
       numberenrolledObj = numberenrolledbox == true ? displayString.getString("Number Enrolled") : ""; 
       dayObj = daybox == true ? displayString.getString("Exam Day") : ""; 
       starttimeObj = startbox == true ? displayString.getString("Start Time") : ""; 
       endtimeObj = endbox == true ? displayString.getString("End Time") : ""; 
       callnumberObj = callbox == true ? displayString.getString("Call Number") : ""; 
       departmentObj = departmentbox == true ? displayString.getString("Department") : ""; 
       instructorObj = instructorbox == true ? displayString.getString("Instructor") : ""; 

       data.exam O = new data.exam(courseObj, buildingObj, roomObj, capacityObj, 
         numberenrolledObj, dayObj, starttimeObj, endtimeObj, callnumberObj, 
         departmentObj, instructorObj); 

       exams.add(O); 
      } catch (Exception E) { 
      } 
     } 
     displayString.close(); 
     con.close(); 
    } catch (Exception e) { 

     if (con != null) { 
      con.close(); 
     } 
     if (stmnt != null) { 
      stmnt.close(); 
     } 
     if (displayString != null) { 
      displayString.close(); 
     } 
     e.printStackTrace(pw); 
    } 

    return exams; 
} 
+0

그냥 지나가는 코멘트 준비된 문에이 tutorial에서 한 번 봐 - 당신은 삼항 연산자에 == 사실 확인 할 필요가 없습니다 (예 : roombox == true), 단순히 roombox를 상태로 만드시겠습니까? ... 읽는 것이 훨씬 쉬워집니다. – 01es

답변

0

몇 가지 :

  • 는이 getAllExamsList 방법에 전달할 수있는 개체를 만듭니다. 이렇게하면 데이터 모델이 정리되고 괴물 같은 매개 변수 목록이 단축됩니다.
public Exam{ 
    private String course; 
    private String building; 
    private String room; 
    //additional variabled you have in the getAllExamsList parameter list 
    public Exam(String course, String building, String room){ 
     this.course=course; 
     this.building=building; 
     this.room=room; 
    } 
    //additional getters and setters 
}