I am using ResultSet.getDate() to get a date field from MSSQL Server database. What variable type do I assign the returned value to? getDate() returns a java.sql.Date object. I am having trouble handling this. Have tried casting to String, but you can't do that !! Any advice would be much appreciated. Thanks.
Hi, rs is the ResultSet object.Actually rs.getDate() return the java.sql.Date not java.util.Date. U have to write like this java.sql.Date resultDate=rs.getDate(2); // 2 or 1 according your requirement U write. Even If U get anyproblem write like this java.sql.Date reultDate=(java.sql.Date)rs.getDate(2);
according to the documentation, java.sql.Date extends java.util.Date. So casting a java.sql.Date to a java.util.Date should be safe(And it works!). I thought the original question was to convert the java.sql.Date to a String that can be displayed to the user in a readable fashion(since he/she tried to cast it to a String). Unfortunately java.sql.Date is not accepted as a parameter for SimpleDateFormat, thus the cast conversion. You really can't do much with a java.sql.Date except write back to the database. Jamie