0

This is the Statement and query of the two date when selected from JDateChooser There is no error but when I select the dates, and click OK the records from the JTable disappears.

enter image description here

Jtable and dates

java.util.Date val1=jDateChooser1.getDate();
java.util.Date val2=jDateChooser2.getDate();
try{
   String sql="select * from Umar where Date between '"+val1+"' and '"+val2+"' ";
   pst=conn.prepareStatement(sql);
   rs=pst.executeQuery();
   jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
   JOptionPane.showMessageDialog(null,e);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    The second date doesn't have a value. Also you should be using a `PreparedStatement` to build your SQL. It allows your to specify parameters for each variable in the SQL and will build the SQL string for you so you will have less syntax errors. – camickr Apr 15 '16 at 14:46
  • Also consider `RowFilter`, for [example](http://stackoverflow.com/q/17854854/230513). – trashgod Apr 15 '16 at 14:52
  • @camickr its saying java.Util.Date can not converted to java.sql.Date – umar tanveer Apr 15 '16 at 19:22

1 Answers1

0

The values pushed in your SQL statement are wrong. It is normal that all rows disappear because no row can match this criteria.

camickr is wright. You should use SQL parameters to pass the values in your SQL. In your case, a valid implementation would be:

java.util.Date val1=jDateChooser1.getDate();
java.util.Date val2=jDateChooser2.getDate();
try{
   String sql="select * from Umar where Date between ? and ? ";
   pst=conn.prepareStatement(sql);
   pst.setDate( 1, val1 );
   pst.setDate( 2, val2 );
   rs=pst.executeQuery();
   jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
   JOptionPane.showMessageDialog(null,e);
}

Of course, the dates must not be null. You should ensure they are not before executing this code.

Laurent Simon
  • 582
  • 4
  • 10