I'm taking in all the information from an MySQL database table using a resultset and adding all the values into an array
public void populateQueueFromDB() {
// create priority queue
try {
String url = "jdbc:mysql://localhost:3306/project";
Connection conn = DriverManager.getConnection(url, "root", "nbuser");
PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication,priority,cores,disk_space,analysis FROM booking");
ResultSet rs;
rs = stmt.executeQuery();
//List<JobRequest> jobList = new ArrayList<JobRequest>();
while (rs.next()) {
JobRequest job = new JobRequest();
User user = new User();
user.setUserID(rs.getString("user_id"));
job.setUserID(user.getUserID()); // changes the /user id to the job.setuser id so can call for my queue print.
job.setStartDate(rs.getString("s_date"));
job.setEndDate(rs.getString("e_date"));
job.setDeadDate(rs.getString("d_date"));
job.setDepartment(rs.getString("department"));
job.setProjectName(rs.getString("projectname"));
job.setProjectApplication(rs.getString("projectapplication"));
job.setPriority(rs.getInt("priority"));
job.setCores(rs.getInt("cores"));
job.setDiskSpace(rs.getInt("disk_space"));
job.setAnalysis(rs.getString("analysis"));
schedulerPriorityQueue.addJob( job );
}
schedulerPriorityQueue.printQueue();
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
from here I go off and call my comparator to order the data, depending on priority of being either 1,2,3 then sort the queue. some other bits of code naming etc but essentially it sends me to the comparator
public class JobQueueComparator implements Comparator<JobRequest> {
@Override
public int compare(JobRequest object1, JobRequest object2) {
if(object1.getPriority() < object2.getPriority()){
return 1;
} else {
return -1;
}
}
}
But the output I'm getting from the comparator is ordering it in priority 3, 1 then 2. I've adapted this from example online but I don't understand the returns on comparator examples I've seen.
How would I be able to change that comparator to sort my priorities, 1 being the most important, 3 being the least. I made sure I'm printing out the output after adding all the result sets into the array so I know it's working as it's changed my ordering around, just dont know how to order it how I want.
Thanks
EDIT: schedulerPriorityQueue
public class Queue {
private Comparator<JobRequest> comparator = new JobQueueComparator(); //calls my comparator
private PriorityQueue< JobRequest> scheduledJobs = new PriorityQueue<JobRequest>(100, comparator);
public void addJob(JobRequest job) {
// now add job to priority queue
scheduledJobs.add(job); // add jobs from the resultset into queue
}
prio1 < prio2,prio1 == prio2,else.schedulerPriorityQueue.addJob()is implemented. That is probably where your error is located. Nor we know if somewhere else you call any sort...