本文介绍了不能在两个日期之间取得记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是我无法获取两个日期之间的所有记录。
My problem is that I can't fetch all records that are between two dates.
我有两个 JDateChooser
s。当我选择'10 -apr-2011'到'20 -apr-2011'这两个日期时,我希望这些日期之间的所有记录显示在我的 JList
中。但是我不能在 JList
中得到任何结果。
I have two JDateChooser
s. When I select two dates like '10-apr-2011' to '20-apr-2011' I want all the records between those dates to be displayed in my JList
. But I can't get any results in the JList
.
我正在使用mysql数据库。
I am using mysql database.
private void Display(java.awt.event.ActionEvent evt) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=
(Connection)DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","ubuntu123");
java.util.Date jd = jDateChooser1.getDate();
java.util.Date jd1 = jDateChooser2.getDate();
// PreparedStatement stmt = (PreparedStatement) con.prepareStatement("select date from invoice where date = ?);
// PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date BETWEEN ' ' AND ' '");
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= '+jd + ' AND date <= '+jd1 + '");
pstmt.execute();
ResultSet rs = pstmt.getResultSet();
int i =0;
DefaultListModel listModel = new DefaultListModel();
while(rs.next())
{
String [] data;
data = new String[100];
data [i] = rs.getString("date");
jList1.setModel(listModel);
listModel.addElement(data [i]);
i = i+1;
}
}
catch (Exception e)
{
System.out.println("2nd catch " + e);
}
}
任何人都可以告诉我我的错误在哪里?
感谢提前..
Can anyone tell me where my mistake is?Thanks in advance..
推荐答案
由于这是一个 PreparedStatement
您可以尝试:
Since this is a PreparedStatement
you can try:
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= ? AND date <= ?");
pstmt.setDate(1,new java.sql.Date(jd.getTime()));
pstmt.setDate(2,new java.sql.Date(jd1.getTime()));
ResultSet rs = pstmt.executeQuery();
这篇关于不能在两个日期之间取得记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!