java.sql.Time对象与java.sql.Date对象混淆 [英] java.sql.Time object being confused with java.sql.Date object

查看:109
本文介绍了java.sql.Time对象与java.sql.Date对象混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询MySQL表并选择varchar,Date,Time,Double和Boolean数据类型。我从ResultSet中获取数据,并将其放在DefaultTableModel中,然后将其放入JTable中,然后放入JScrollPane进行显示。



除了具有java.sql.Time对象的列之外,所有数据都正确显示。这些显示为日期对象,并且它们都具有1970年1月1日的值。



似乎java.sql.Time对象正在读取为java .sql.Date对象(我猜,1970年1月1日被返回,因为时间值超出了对日期对象有效的范围)。



我很困惑,因为如果我重写DefaultTableModel中的getColumnClass方法来总是返回String.class,并使用表方法setAutoCreateRowSorter(true),当我点击标题的列包含要排序的java.sql.Time对象,抛出一个java.lang.ClassCastException,它表示java.sql.time不能被转换为String。为什么这样将数据正确地标识为java.sql.Time,但是如果我重写DefaultTableModel getColumnClass以返回正确的类,它会将其视为日期而不是时间?任何解决问题的帮助将不胜感激。这是我如何在DefaultTableModel中覆盖他的getColumnClass方法:

  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; //返回字符串,不管什么类是
}
}
};


解决方案

ClassCastException即将到来,因为您尝试投射时间类作为一个字符串。它显示为Date对象,因为java.sql.Time和java.sql.Date都是java.util.Date的子类。这些子类只是简单的包装器(只需要足够的额外信息即可映射到SQL列中)。所以在Swing JTable中,它被用作java.util.Date,因为java.sql类型大部分与swing组件无关。


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
                }
            }
        };

解决方案

The ClassCastException is coming because you are trying to cast the Time class as an String. It appears as an Date object because java.sql.Time and java.sql.Date are both subclasses of java.util.Date. These subclasses are just thin wrappers (Same thing with just enough extra information to be mapped in to SQL columns). So in a Swing JTable it's being used as java.util.Date since java.sql types are irrelevant to swing components for the most part.

这篇关于java.sql.Time对象与java.sql.Date对象混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆