java - jdatechooser as parameter in mysql query -
what correct method can query statement date jdatechooser parameter
defaulttablemodel model= (defaulttablemodel)displayrecievetb.getmodel(); displayrecievetb.revalidate(); model.getdatavector().removeallelements(); try{ string date = datechooserrs.getdate().tostring(); stmt = conn.createstatement(); stmt.executequery("select *from supplyrecievable recievedate = '"+date+"' "); rs = stmt.getresultset(); while (rs.next()) { string r1 = rs.getstring("itemname"); string r2 = rs.getstring("itemgroup"); string r3 = rs.getstring("itemcount"); string r4 = rs.getstring("totalcost"); string r5 = rs.getstring("itemcode"); model.addrow(new object[] {r5,r1,r2,r3,r4}); displayrecievetb.revalidate(); } } catch(exception e) { joptionpane.showmessagedialog(null, e.getmessage()); }
- use
preparedstatement
- don't convert
date
valuestring
, resulting values incompatiable (and if database column other date or time stamp type, you're doing wrong)
for example...
defaulttablemodel model = (defaulttablemodel) displayrecievetb.getmodel(); displayrecievetb.revalidate(); model.getdatavector().removeallelements(); try { date date = datechooserrs.getdate(); try (preparedstatement stmt = conn.preparestatement("select * supplyrecievable recievedate = ?")) { // can use java.sql.timestamp if column of correct type stmt.setdate(1, new java.sql.date(date.gettime())); try (resultset rs = stmt.executequery()) { while (rs.next()) { string r1 = rs.getstring("itemname"); string r2 = rs.getstring("itemgroup"); string r3 = rs.getstring("itemcount"); string r4 = rs.getstring("totalcost"); string r5 = rs.getstring("itemcode"); model.addrow(new object[]{r5, r1, r2, r3, r4}); // pointless, model should notifty table needs updated //displayrecievetb.revalidate(); } } } } catch (exception e) { joptionpane.showmessagedialog(null, e.getmessage()); }
this assume database column type compatible java.sql.date
value
see using prepared statements more details
Comments
Post a Comment