2012-04-24 70 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

只是一筆帶過的評論 - 你不需要檢查==真在三元操作符(如roombox == true),只需說明roombox? ...它使閱讀起來更容易。 – 01es 2012-04-24 17:33:09

回答

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 
} 
  • 拿上準備好的語句來看看這個tutorial