Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:46:33+00:00 2026-06-03T00:46:33+00:00

I am working with user-input and created a filter for page. It is working,

  • 0

I am working with user-input and created a filter for page. It is working, but I understand that I query database directly.

Can this query be optimized to work as prepared query ? I decided to check with pros before I start typing bunch of if-else statements.

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:46:35+00:00Added an answer on June 3, 2026 at 12:46 am

    A few things to help you out:

    • Create an object that you can pass to this getAllExamsList method. This will organize your data model and shorten the monstrous parameter list.
    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
    }
    
    • Take a look at at this tutorial on prepared statements
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an updates page where the user can input updates into
I am working on semantic web project where user will input text query like
I'm working on a simple program that collects and checks user-input. In addition to
I have created the output for a program that allows a user to input
While working on filtering my NSMutableDictionary based on user input, I created the following
I'm working on a translator that will take English language text (as user input
I'm working on a program where user input is an array of mixed Strings
I am working on BlackBerry user interface. But the BlackBerry UIs are less attractive
The application I've been working on takes user input from a form, and builds
I have created a simple ASP.Net application, where first page accepts an input and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.