Im implementing a subquery in java which requires 2 parameters to be fed using a prepared statement.
Passing parameter within the inner query is working but then parameter is not passed to the outer one.
During execution i get an error on stacktrace saying :No operations allowed after statement closed. and hence the prepared statement returns wrong results.Question: How best can i pass the parameter successfully to the outer table.
Below is a sample code of my implementation
pst = conn.prepareStatement("SELECT slot.time_slot_id,slot.`date`,slot.count FROM
(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue
RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id
WHERE ue.status= 1 AND event_id=?
GROUP BY ue.`date`,time_slot_id
) AS slot WHERE slot.count >=? ");
pst.setInt(1,eventId);
//here is the parameter that is passed to the outer table
pst.setInt(2,count);
rs = pst.executeQuery();
here is an update for the entire method am implementing as below;
public static DefaultListModel Fetch(int eventId){
DBconnection.connect();
DefaultListModel fetchedSlots= new DefaultListModel();
try{
//
pst = conn.prepareStatement("SELECT slot.time_slot_id,slot.`date`,slot.count FROM \n" +
"(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue \n" +
"RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id\n" +
"WHERE ue.status= 1 AND event_id=?\n" +
"GROUP BY ue.`date`,time_slot_id) AS slot\n" +
"WHERE slot.count >? ");
pst.setInt(1,eventId);
//here a function is invocked using eventId to get counts
pst.setInt(2,FetchCounts(eventId));
rs = pst.executeQuery();
while(rs.next()){
fetchedSlots.addElement(new ModelSlot(rs.getInt("time_slot_id"),rs.getString("date")));
}
DBconnection.CloseConnection();
}catch (SQLException ex){
System.out.println(ex.getMessage());
}
return fetchedSlots;
}