什么是正确的方法,所以我可以查询这个语句,其中jdatechooser的日期将是我的参数
DefaultTableModel model= (DefaultTableModel)DisplayRecieveTB.getModel();
DisplayRecieveTB.revalidate();
model.getDataVector().removeAllElements();
try{
String date = DateChooserRS.getDate().toString();
stmt = conn.createStatement();
stmt.executeQuery("SELECT *FROM supplyrecievable where 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());
}
答案 0 :(得分:1)
PreparedStatement
Date
值转换为String
,结果值可能不兼容(如果数据库列是日期或时间戳类型之外的任何其他值,那么您就是做错了)例如......
DefaultTableModel model = (DefaultTableModel) DisplayRecieveTB.getModel();
DisplayRecieveTB.revalidate();
model.getDataVector().removeAllElements();
try {
Date date = DateChooserRS.getDate();
try (PreparedStatement stmt = 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, 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});
// 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());
}
假设数据库列类型与java.sql.Date
值
有关详细信息,请参阅Using Prepared Statements