日期格式错误java.sql.SQLException:列类型无效

Jåc*_*cob 6 java jdbc primefaces jsf-2

我正在使用JSF显示日期pattern="dd-MMM-yyyy".

当我尝试将日期值插入/更新到我的oracle DB中时,我得到了

java.sql.SQLException: Invalid column type
Run Code Online (Sandbox Code Playgroud)

因为插入或更新之前的日期格式是这种格式

Wed Feb 09 00:00:00 AST 2011
Run Code Online (Sandbox Code Playgroud)

如何正确地将我的日期值插入或更新到Oracle Db以及执行此操作的最佳方法是什么?

更新1

我的db插入代码.

private void editSchedule(Schedule schedule)
        Object[] values = { schedule.getStartDate(),
                schedule.getVacationId() };             
        Connection connection = null;       
        PreparedStatement preparedStatement = null; 
        try {                           
            connection = datacon.getConnection();               
            preparedStatement = prepareStatement(connection, SQL_EDIT, values);         
            preparedStatement.executeUpdate();          

        } catch (Exception e) {
            logger.info("errro "+e.getMessage());
            e.printStackTrace();
        } finally {
            // TODO: handle exception
            close(connection, preparedStatement);
        }

    }
Run Code Online (Sandbox Code Playgroud)

PreparedStaement代码部分

public static PreparedStatement prepareStatement
        (Connection connection, String sql, Object... values)
            throws SQLException
    {
        PreparedStatement preparedStatement = connection.prepareStatement(sql
            );
        setValues(preparedStatement, values);
        return preparedStatement;
    }

    public static void setValues(PreparedStatement preparedStatement, Object... values)
        throws SQLException
    {
        for (int i = 0; i < values.length; i++) {
            preparedStatement.setObject(i + 1, values[i]);
            logger.info("sql  "+Arrays.asList(values));
        }
    }
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

JDBC只能理解java.sql.Date,java.sql.Timejava.sql.Timestamp不是SQL列类型java.util.Date.

你需要改变

    Object[] values = { schedule.getStartDate(),
            schedule.getVacationId() };             
Run Code Online (Sandbox Code Playgroud)

通过

    Object[] values = { new java.sql.Date(schedule.getStartDate().getTime()),
            schedule.getVacationId() };             
Run Code Online (Sandbox Code Playgroud)

然后它会工作.继续java.util.Date在你的模型中使用.JSF反过来也不明白java.sql.Date.


Jon*_*eet 5

当您插入/更新时,听起来您正试图将数据包含为文本.不要那样做 - java.sql.Date在a中使用a PreparedStatement.引入不必要的字符串转换是一个非常糟糕的主意 - 它会使您的代码变得非常脆弱,并使代码更加混乱:只要您愿意,就可以将数据保存在适当的数据类型中.