hi, this is a simple question, but yet i get confused all the time and i need clarification.
What like to know is what is the best way to convert a date value form (String--->Date).
The other problem is that i did use the [Date] object in accessing my database (MS Access) with this quere:
String quere = "SELECT * FROM Table WHERE Date BETWEEN '"+fromdate+"' AND '"+ todate+"'";
where, fromdate & todate are both [Date] objects, but with no results...
If any one can give me a model of how to use the [Date] object with the Database Date/Time fields.
Thanks
-
The Date problem... (3 messages)
- Posted by: zalaqa toosh
- Posted on: December 18 2003 21:27 EST
Threaded Messages (3)
- The Date problem... by stephen smithstone on December 19 2003 03:49 EST
- The Date problem... by rock zhang on December 19 2003 11:27 EST
- The Date problem... by Paul Strack on December 19 2003 13:12 EST
-
The Date problem...[ Go to top ]
- Posted by: stephen smithstone
- Posted on: December 19 2003 03:49 EST
- in response to zalaqa toosh
i *think* as you are using MS Access the data values need to go between #'s
so BETWEEN #<fromdate># -
The Date problem...[ Go to top ]
- Posted by: rock zhang
- Posted on: December 19 2003 11:27 EST
- in response to zalaqa toosh
SimpleDateFormat can convert String to Date or convert date to string easyly..
if you use ms access ,the date param must be String(MM/dd/yyyy) adn
closed by '#' and '# ' -
The Date problem...[ Go to top ]
- Posted by: Paul Strack
- Posted on: December 19 2003 13:12 EST
- in response to zalaqa toosh
For your query, use a PreparedStatement rather than a Statement object. You should also convert from java.util.Date to java.sql.Date:
// Convert dates
java.util.Date fromDate = ...
java.util.Date toDate = ...
java.sql.Date fromSQLDate = new java.sql.Date(fromDate.getTime());
java.sql.Date toSQLDate = new java.sql.Date(toDate.getTime());
// Use PreparedStatement
String sql = "SELECT * FROM Table WHERE Date BETWEEN ? AND ?"
PreparedStatement ps = connect.prepareStatement(sql);
ps.setDate(1, fromSQLDate); // Replaces first "?"
ps.setDate(2, toSQLDate); // Replaces second "?"
ResultSet results = ps.executeQuery();
One big advantage of a PreparedStatement is that it will automatically make the database-specific translations for your Date objects (and other SQL datatypes).