日期格式错误java.sql.SQLException:无效的列类型 [英] Date Format Error java.sql.SQLException: Invalid column type
问题描述
我正在使用pattern="dd-MMM-yyyy"
在JSF中显示日期.
I am displaying date in JSF using pattern="dd-MMM-yyyy"
.
当我尝试将日期值插入/更新到oracle数据库时,我得到
When I am trying to insert/update date values into my oracle DB, I am getting
java.sql.SQLException: Invalid column type
因为我插入或更新之前的日期格式是这种格式
because my date format before insert or update is in this format
Wed Feb 09 00:00:00 AST 2011
如何正确地将日期值插入或更新到Oracle Db,什么是最好的方法?
How can I correctly insert or update my date values to Oracle Db and what is the best approach for doing this?
更新1
我的数据库插入代码.
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);
}
}
PreparedStaement代码部分
PreparedStaement code part
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));
}
}
推荐答案
听起来,您在插入/更新时试图将数据作为 text 包含在内.不要这样做-在PreparedStatement
中使用java.sql.Date
.引入不必要的字符串转换是一个非常糟糕的主意-它使您的代码非常脆弱,并使代码更加混乱:尽可能将数据保留在适当的数据类型中.
It sounds like you're trying to include the data as text when you're inserting/updating. Don't do that - use a java.sql.Date
in a PreparedStatement
. Introducing unnecessary string conversions is a really bad idea - it makes your code very brittle, and makes the code more confusijng: keep your data in an appropriate data type as long as you possibly can.
这篇关于日期格式错误java.sql.SQLException:无效的列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!