【问题标题】:Convert Timestamp from Database to java.sql.Timestamp将时间戳从数据库转换为 java.sql.Timestamp
【发布时间】:2014-10-23 06:47:48
【问题描述】:

我正在从数据库中查询数据,并希望将数据库时间戳转换为 java.sql.Timestamp:

    List<Map<String, Object>> rows = jdbcTemplate.queryForList(SELECT_ALL_DATA);    
    for(Map row : rows) {
                Customer customer = new Customer();
                customer.setMaturityDate(new Timestamp((Long) row.get("MATURTIY_DATE")));
    }

但是,这样做会给我:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Long

我也尝试过不使用 Long 的演员表,但这给了我:

The constructor Timestamp(Object) is undefined

关于如何转换数据库对象有什么建议吗?

感谢您的回复!

【问题讨论】:

    标签: java database timestamp


    【解决方案1】:

    您不需要创建新的时间戳实例,因为它已经返回一个:

    List<Map<String, Object>> rows = jdbcTemplate.queryForList(SELECT_ALL_DATA);    
    for(Map row : rows) {
                Customer customer = new Customer();
                customer.setMaturityDate((Timestamp)row.get("MATURTIY_DATE"));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2012-04-04
      • 2016-10-07
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多