Jdatechooser As Parameter In Mysql Query
what is the correct method so i can query this statement where the date from the jdatechooser will be my parameter DefaultTableModel model= (DefaultTableModel)DisplayRecieveTB.get
Solution 1:
- Use a
PreparedStatement - Don't convert the
Datevalue toString, the resulting values are likely to be incompatiable (and if the database column is anything other then a date or time stamp type, then you're doing it wrong)
For example...
DefaultTableModelmodel= (DefaultTableModel) DisplayRecieveTB.getModel();
DisplayRecieveTB.revalidate();
model.getDataVector().removeAllElements();
try {
Datedate= DateChooserRS.getDate();
try (PreparedStatementstmt= conn.prepareStatement("SELECT * FROM supplyrecievable where recievedate = ?")) {
// You can also use a java.sql.Timestamp if the column is of the correct type
stmt.setDate(1, newjava.sql.Date(date.getTime()));
try (ResultSetrs= stmt.executeQuery()) {
while (rs.next()) {
Stringr1= rs.getString("itemname");
Stringr2= rs.getString("itemgroup");
Stringr3= rs.getString("itemcount");
Stringr4= rs.getString("totalcost");
Stringr5= rs.getString("itemcode");
model.addRow(newObject[]{r5, r1, r2, r3, r4});
// This is pointless, as the model should notifty the table it needs to be updated//DisplayRecieveTB.revalidate();
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
This is assume the database column type is compatible with the java.sql.Date value
See Using Prepared Statements for more details
Post a Comment for "Jdatechooser As Parameter In Mysql Query"