问题
My problem is that I can't fetch all records that are between two dates.
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
.
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..
回答1:
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();
回答2:
You need to take out jList1.setModel(listModel);
out of the loop
while(rs.next())
{
String [] data;
data = new String[100];
data [i] = rs.getString("date");
//jList1.setModel(listModel);
listModel.addElement(data [i]);
i = i+1;
}
jList1.setModel(listModel);
回答3:
You need to alter your query. Something like this:-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String jdStr = sdf.format(jd);
String jd1Str = sdf.format(jd1);
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("SELECT date FROM invoice WHERE date >= '" + jdStr + "' AND date <= '" + jd1Str + "'");
Previously, in your query, the 2 parameters, jd
& jd1
were not getting append. This change will now append it in the query. The problem was with the jd & jd1
not correctly being appended in the query.
Note:- I've added a SDF so that you could format your date in format needed and append it to the query.
回答4:
never to create an
GUI Objects
inside hard and long runningJDBC
, nor insidetry
-catch
-finally
, on exception those Object never will be createdDefaultListModel listModel = new DefaultListModel();
should be created an local variable, and then isn't required to recreate a newXxxListModel
on runtimehave to remove all elements from
listModel
, otherwise newItems
will be appended to the end ofJList
definition for
String [] data;
anddata = new String[100];
anddata [i] = rs.getString("date");
insidewhile(rs.next()) {
are quite useless, because database records are stored from this array inXxxListModel
, and accessible for other usage for elsewhereConnection
,PreparedStatement
andResultSet
should be closed infinally block
(try
-catch
-finally
), otheriwe theseObjects
stays (and increasing) inJVM
memory,
回答5:
are you sure dates in SQL and date from java.util.Date are in the same format?
Try using
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
to check whenever they are same.
jd and jd1 by default would differ by SQL date @see SQL Dates
回答6:
This is not the correct way of using PreparedStatement
, which is already prepared to handle Dates, so you should not covert them to String. Your code should look like this:
PreparedStatement pstmt = ...
pstmt.setDate(...)
Also your query String is not really using the jd as a variable, you misused ' and "
来源:https://stackoverflow.com/questions/16165759/cant-get-records-between-two-dates