我迷路了,试图从两个jDateChoosers
然后查询德比数据库表“TEST5”列“DATEDATE”中取日期,列出这两个日期之间的所有记录到我的jTable。
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String url = "jdbc:derby://localhost:1527/test5";
String user = "APP";
String pass = "APP";
Connection con = null;
try {
con = DriverManager.getConnection(url ,user, pass);
} catch (SQLException ex) {
Logger.getLogger(MainDisplay.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date dateStart = new java.sql.Date(jDateChooser2.getDate().getTime());
java.sql.Date dateEnd = new java.sql.Date(jDateChooser3.getDate().getTime());
try (PreparedStatement st = con.prepareStatement("SELECT DateDate FROM TEST5 WHERE DateDate between ? AND ?" );
ResultSet rs = st.executeQuery()) {
st.setDate(1, dateStart);//set start date
st.setDate(2, dateEnd);//set end date
MainDisplay.jTable1.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
con.close();
} catch (SQLException ex) {
Logger.getLogger(MainDisplay.class.getName()).log(Level.SEVERE, null, ex);
}
这是我的最新修订版(抱歉,不确定在哪里发布回复)
答案 0 :(得分:2)
您没有将任何值设置为您必须使用的dateStart >= ? AND dateEnd <= ?
:
try (PreparedStatement st = con.prepareStatement("SELECT DateDate FROM TEST5 WHERE dateStart >= ? AND dateEnd <= ?");) {
st.setDate(1, dateStart);//set start date
st.setDate(2, dateEnd);//set end date
ResultSet rs = st.executeQuery()
MainDisplay.jTable1.setModel(DbUtils.resultSetToTableModel(rs));
另外,请确保设置列的正确名称DateDate, dateStart, dateEnd
修改强>
如果我理解,在您的情况下,您的表格只有一个日期,并且您希望获得此日期的所有日期<= startDate
和>= endDate
,在这种情况下您可以使用:
SELECT DateDate FROM TEST5 WHERE DateDate >= ? AND DateDate <= ?
这与此between
的相似之处:
SELECT DateDate FROM TEST5 WHERE DateDate between ? AND ?