java - Oracle 日期的无效年份异常

标签 java oracle jdbc

我正在尝试将日期保存到我的 Oracle 数据库中。

    long startTime = System.nanoTime();
    java.sql.Date startTimeDisplay = new Date(startTime);

    PreparedStatement st;
    String sql = "INSERT INTO J0T_JOB_STATUS (JST_JOB_ID, JST_JOB_CONFIGURATION_ID, JST_STATUS_CD, JST_START_TM, JST_END_TM, JST_CURR_STEP, JST_STEP_DETAILS, JST_MSG_COLLECTION) "
            + "VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
    st.setDate(4, startTimeDisplay);

这在我们使用 HSQL 而不是 Oracle 的 JUnit 测试套件中运行得很好,但是当使用 Oracle 时我得到:

    [err] Exception in thread "Thread-15" 
    [err] java.lang.IllegalArgumentException: Invalid year value
    [err]   at oracle.sql.TIMESTAMP.getOracleYear(TIMESTAMP.java:772)
    [err]   at oracle.sql.DATE.toBytes(DATE.java:533)
    [err]   at oracle.sql.DATE.<init>(DATE.java:121)
    [err]   at oracle.jdbc.driver.OraclePreparedStatement.setDate(OraclePreparedStatement.java:8747)
    [err]   at oracle.jdbc.driver.OraclePreparedStatementWrapper.setDate(OraclePreparedStatementWrapper.java:177)
    [err]   at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.setDate(WSJdbcPreparedStatement.java:1333)
etc.

表定义:

desc J0T_JOB_STATUS
Name                     Null Type         
------------------------ ---- ------------ 
JST_JOB_ID                    VARCHAR2(50) 
JST_JOB_CONFIGURATION_ID      NUMBER       
JST_STATUS_CD                 VARCHAR2(30) 
JST_START_TM                  TIMESTAMP(6) 
JST_END_TM                    TIMESTAMP(6) 
JST_CURR_STEP                 NUMBER       
JST_STEP_DETAILS              CLOB         
JST_MSG_COLLECTION            CLOB         

最佳答案

根据Java文档关于System.nanoTime()(我假设是Java7,因为你没有提供你的java版本):

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative)

链接:https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()

我建议使用System.currentTimeMillis(),除非您想最终保留负数。

关于java - Oracle 日期的无效年份异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304957/

相关文章:

java - 如何在另一个类的不同包中创建一个类的对象

Javapoet:引用其他生成的类

oracle - PL/SQL ORA-01422 : exact fetch returns more than requested number of rows

java - 如何循环访问数据库并打印表中的所有项目

Java 和 MySQL : More than 'max_user_connections' exception

java - 在Java中,我可以在每次事件分派(dispatch)线程从处理输入事件返回时调用一个例程吗?

java - 任意键的锁定处理程序

java - Oracle JDBC 间歇性连接问题

java.sql.SQLException : ORA-01704: string literal too long When Insert Or Update

java - 无法从android中的远程ubuntu tomcat服务器连接mysql数据库