日期格式错误java.sql.SQLException:列类型无效
发布时间:2020-05-24 14:23:45 所属栏目:Java 来源:互联网
导读:我使用pattern =“dd-MMM-yyyy”在JSF中显示日期. 当我尝试将日期值插入/更新到我的oracle DB中时,我得到了 java.sql.SQLException: Invalid column type 因为插入或更新之前的日期格式是这种格式 Wed Feb 09 00:00:00 AST 2011 如何正确地将我的日期值插入或
|
我使用pattern =“dd-MMM-yyyy”在JSF中显示日期. 当我尝试将日期值插入/更新到我的oracle DB中时,我得到了 java.sql.SQLException: Invalid column type 因为插入或更新之前的日期格式是这种格式 Wed Feb 09 00:00:00 AST 2011 如何正确地将我的日期值插入或更新到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);
}
}
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));
}
}
解决方法当您插入/更新时,听起来您正试图将数据包含为文本.不要这样做 – 在PreparedStatement中使用java.sql.Date.引入不必要的字符串转换是一个非常糟糕的主意 – 它会使您的代码变得非常脆弱,并使代码更加混乱:只要您愿意,就可以将数据保存在适当的数据类型中.(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
