-1

How can i get value from 2 JDateChooser formatted yyyy-MM-d and put it in sql query (String sql="select .... from ... between date1 and date2") which should search data between those dates and outputted in a JTable.

I used code but no output appeared:

String v1 = gr_date1.getDate().toString();
String v2 = gr_date2.getDate().toString();
try {
    sql = "select barcode,itemName,description,supplier,capital,wholesale,srp,minStock,status,dateSaved from item where dateSaved between '"+v1+"' AND '"+v2+"'";
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    gReport_table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
DeadLuck
  • 59
  • 1
  • 1
  • 10
  • First off, don't ever, EVER, build SQL queries like this. It may not be a problem at present because of how the `v1` and `v2` strings are built, but give it some time and by Murphy's law someone *will* eventually change it so it's susceptible to SQL injections: http://en.wikipedia.org/wiki/SQL_injection I strongly encourage you to instead use a query building library that safely escapes parameters to prevent SQL injections. – Emil Lundberg Jun 09 '13 at 09:54
  • its fix thanks anyway by the way sir this just an example coding but maybe there will be enhancing in our further lessons – DeadLuck Jun 09 '13 at 10:17

1 Answers1

0

Your passing v1 and v2 as a string.Then it will not compare

try DateTime.Parse() for v1 and v2

DateTime.Parse(v1) and DateTime.Parse(v2)

Deepak Saralaya
  • 457
  • 4
  • 12
  • Date dateFromDateChooser = gr_date1.getDate(); String v1 = String.format("%1$tY-%1$tm-%1$td", dateFromDateChooser); I tried this and it works .. Thank you anyway. – DeadLuck Jun 09 '13 at 10:04