无法设置从Java到Oracle的微秒值

sgtfey8w  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(137)

我有一个从当前时间创建的日期,对象类型是XMLGregorianCalendar。在Java中,我可以看到我的日期有微秒,但在SQL中,它被设置为0。到底是什么问题呢?

public static XMLGregorianCalendar initializeTime(LocalDateTime localDateTime) {
        try {
            XMLGregorianCalendar time= DatatypeFactory.newInstance()
              .newXMLGregorianCalendar(String.valueOf
              (LocalDateTime.now().truncatedTo(ChronoUnit.MICROS)));
            
        return time;
        } catch (DatatypeConfigurationException var3) {
            throw new RuntimeException("Execution time could not created.");
        }
    }

在Java中,它显示:2023-05-24T15:50:07.931456
但是在SQL中(类型是TIMESTAMP(6)),它显示如下:2023年5月24日15.50.07.931000000 PM
SQL调用,你可以在下面找到它。dummytest是一个过程,基本上是获取一个参数并将该参数插入到表中。

<select id="add_time" statementType="CALLABLE" parameterType="java.util.Map">
        {call dummytest(
                #{time, mode=IN, jdbcType=TIMESTAMP}
            )}
    </select>

程序代码:

create or replace procedure dummytest(p_timestamp TIMESTAMP) is
begin
  insert into testtimestamp values (p_timestamp);
  commit;
end ;

我失去了微秒的精度。我必须使用XMLGregorianCalendar,因为所有代码都是为这个API设计的。

wlzqhblo

wlzqhblo1#

这个问题是通过java端的自定义处理程序解决的。

<select id="add_time" statementType="CALLABLE" parameterType="java.util.Map">
        {call dummytest(
                #{time, mode=IN, jdbcType=TIMESTAMP, typeHandler = com.project.yunus.XMLGregorianCalendarWithFractionalTypeHandler}
            )}
    </select>

XMLGregorianCalendarWithFractionalTypeHandler的内容:

public class XMLGregorianCalendarWithFractionalTypeHandler implements TypeHandler {
    public XMLGregorianCalendarWithFractionalTypeHandler() {
    }

    public void setParameter(PreparedStatement paramPreparedStatement, int paramInt, Object paramObject, JdbcType paramJdbcType) throws SQLException {
        if (paramObject == null) {
            paramPreparedStatement.setNull(paramInt, 93);
        } else {
            if (!(paramObject instanceof XMLGregorianCalendar)) {
                throw new IllegalArgumentException("Excepted XMLGregorianCalendar, but have: " + paramObject);
            }

            XMLGregorianCalendar objectCalendar = ((XMLGregorianCalendar)paramObject);
            Timestamp timestamp = Timestamp.valueOf(objectCalendar.toString().replace("T", " "));
            paramPreparedStatement.setTimestamp(paramInt, timestamp);
        }

    }

    public Object getResult(ResultSet rs, String columnLabel) throws SQLException {
        Timestamp value = rs.getTimestamp(columnLabel);
        return rs.wasNull() ? null : stringToXMLGregorianCalendar(value);
    }

    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        Timestamp value = rs.getTimestamp(columnIndex);
        return rs.wasNull() ? null : stringToXMLGregorianCalendar(value);
    }

    public Object getResult(CallableStatement rs, int columnNb) throws SQLException {
        Timestamp value = rs.getTimestamp(columnNb);
        return rs.wasNull() ? null : stringToXMLGregorianCalendar(value);
    }

    private static XMLGregorianCalendar stringToXMLGregorianCalendar(Timestamp value) {
        try {
            return dateToXMLGregorianCalendar(value);
        } catch (Exception var2) {
            return null;
        }
    }

    public static XMLGregorianCalendar dateToXMLGregorianCalendar(Timestamp timestamp) throws DatatypeConfigurationException {
        return  DatatypeFactory.newInstance().newXMLGregorianCalendar(timestamp.toString().replace(" ", "T"));

    }

相关问题