I am querying a MySQL table and selecting varchar, Date, Time, Double, and Boolean data types. I take the data from the ResultSet and put it in a DefaultTableModel, which then gets put in a JTable, and then a JScrollPane for displaying.
All of the data is displayed correctly except the columns with java.sql.Time objects in them. These are being displayed as a date object, and they all have the value of Jan 1, 1970.
It seems that java.sql.Time objects are being read as java.sql.Date objects (I am guessing Jan 1, 1970 is being returned because the time value is outside of the range that would be valid for a date object).
I am confused because if I override the getColumnClass method in the DefaultTableModel to always return String.class and use the table method setAutoCreateRowSorter(true), when I click on the header of a column containing java.sql.Time objects to sort it, a java.lang.ClassCastException is thrown, and it says that java.sql.time can't be cast as a String. Why does this correctly identify the data as java.sql.Time, but if I override the DefaultTableModel getColumnClass to return the correct class, it sees it as a date instead of a time? Any help resolving the issue would be greatly appreciated. Here is how I am overriding he getColumnClass method in the DefaultTableModel:
model = new DefaultTableModel() {
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == numberOfColumns) {
return Boolean.class;
} else {
return getValueAt(1, columnIndex).getClass(); //return actual class
return String.class; //return string regardless of what class is
}
}
};