Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anybody have experience with getDate(int) 1

Status
Not open for further replies.

cmmrfrds

Programmer
Feb 13, 2000
4,690
US
I have tried many different things to read a date field from an MS Access database. Does getDate(int) or getDate(int, Calendar) work with other databases? The error message says that the &quot;Type is DATETIME&quot;. I tried using the escape syntax suggested in one book but I get an error on the SQL clause.<br><br>my latest code:<br>java.sql.Date sdate = null;<br>java.util.Calendar cal = java.util.Calendar.getInstance();<br>java.util.TimeZone tz&nbsp;&nbsp;= java.util.TimeZone.getTimeZone(&quot;CST&quot;);<br>cal.setTimeZone(tz);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( int i = 1; i &lt;= rsmd.getColumnCount(); ++i )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch( rsmd.getColumnType( i ) ) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Types.DATE:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentRow.addElement(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sdate = (java.sql.Date)(rs.getDate( i , cal) ) );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br><br>I would appreciate any help or references.<br><br>Thank you,<br>Jerry
 
Dear Jerry,<br><br>Here is what works on my system where the target table in MS Access has a column named &quot;DT&quot; that is a type DateTime.<br><br>ResultSet rs = stmt.executeQuery(sql);<br>if ( rs.next()){<br>&nbsp;&nbsp;java.sql.Date dt = rs.getDate(&quot;DT&quot;);<br>&nbsp;&nbsp;System.out.println( &quot;Today is: &quot; + dt.getMonth() + &quot;/&quot; + <br>&nbsp;&nbsp;&nbsp;&nbsp;dt.getDate() +<br>&nbsp;&nbsp;&nbsp;&nbsp;&quot;/&quot; + dt.getYear());<br>}<br>rs.close();<br>con.close();<br><br>Hope this helps<br>-pete<br>
 
Thank you Pete. Your example helped in two ways it showed me a correct syntax and confirmed that it would work in MS Access. <br><br>I am able to get the date now. I changed my code to TIMESTAMP since I originally stored the date as a datetime field. I still have a problem where the date is null. The same code gets a nullpointerexception if my date is null. Can you show me some code to handle a null date?<br><br>I have a changedDate in my database that gets updated if the record is changed but not initially so lots of records have null in the date.<br><br>Here is the snippet of my working code:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java.sql.Timestamp ts = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Types.TIMESTAMP:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ts = ( rs.getTimestamp( i ) );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentRow.addElement(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new String (&nbsp;&nbsp;ts.toString() ));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br><br>The null exception is on the ts.toString when the date is null. The things I tried didn't work. To test I went back to my table and only selected records where the date was not null and then the code works fine.<br><br>again, thank you for the help.<br>Jerry
 
Dear Jerry,<br><br>Try this, I didn't test it.<br><br>case Types.TIMESTAMP:<br>ts = ( rs.getTimestamp( i ) );<br>if ( null != ts){<br>&nbsp;&nbsp;currentRow.addElement(<br>&nbsp;&nbsp;&nbsp;&nbsp;new String (&nbsp;&nbsp;ts.toString() ));<br>}else{<br>&nbsp;&nbsp;currentRow.addElement( new String(&quot;null&quot;));<br>}<br><br>break;<br>
 
Thank you Pete. It worked fine the way you coded it. I wasn't sure how to check an object for
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top