My Java code is this:
public void FillCombo_Disp(){
java.sql.Date fromDate = new java.sql.Date(jDateChooser_Date1.getDate().getTime());
java.sql.Date toDate= new java.sql.Date(jDateChooser_Date2.getDate().getTime());
String space = jComboBox_Space.getSelectedItem().toString();
String [] space_split = space.split(" ");
String id_space = space_split[0];
String query = "SELECT ID FROM Rent WHERE ID_Space = '" + id_space + "' AND Date1 BETWEEN ? AND ? OR Dat2 BETWEEN ? AND ?";
try {
pst = conn.Connection().prepareStatement(query);
pst.setDate(1, fromDate);
pst.setDate(2, toDate);
pst.setDate(3, fromDate);
pst.setDate(4, toDate);
ResultSet res = pst.executeQuery();
int ID = 0;
while(res.next()) {
ID = res.getInt("ID");
}
if(ID!=0)
jTextField_Disp.setText("Rented");
else
jTextField_Disp.setText("Free");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
My function is to control two dates entered by a user and compare them to the database. If the dates are within the range of time you should give me back the id_rent. But I don't understand why cannot enter in while loop:
while(res.next()) {
ID = res.getInt("ID");
}
Help me!
ANDoperator has a higher order of precedence than theORoperator. e.g. "fee AND fi OR fo" is equivalent to "(fee AND fi) OR fo". I suspect you want to add some parens: "fee AND (fi OR fo)'. I'm also curious why the value ofid_spaceis included as a literal in the SQL text, rather than a bind placeholder (like the date values.) I suggest that you verify that your SQL statement is working correctly (from another client).