String query1 =“insert into ORDER_T(ORDER_ID,ordernumber,ordertotal,ORDERDESCRIPTION,CREATEDDATE,” +“UPDATEDDATE”值(?,?,?,?,TO_DATE(?,'dd / mm / yyyy'),TO_DATE(?,'dd / mm / yyyy'))“;
preparedSt=con.prepareStatement(query1);
preparedSt.setString(1, order.getOrderId());
preparedSt.setString(2, order.getOrderNumber());
preparedSt.setString(3, order.getOrderDescription());
preparedSt.setBigDecimal(4, order.getOrderTotal());
preparedSt.setDate(5,(java.sql.Date) order.getCreatedDate());// java.util.Date cannot be cast to java.sql.Date
preparedSt.setDate(6, (java.sql.Date) order.getUpdatedDate());// java.util.Date cannot be cast to java.sql.Date
result=preparedSt.executeUpdate();
控制台 连接成功!! java.lang.ClassCastException:java.util.Date无法强制转换为java.sql.Date 在com.cloudbill.order.process.OrderProcessorImpl.placeOrder(OrderProcessorImpl.java:129) 在com.cloudbill.order.bo.OrderMain.main(OrderMain.java:174) java.lang.ClassCastException:java.util.Date无法强制转换为java.sql.Date 在com.cloudbill.order.process.OrderProcessorImpl.placeOrder(OrderProcessorImpl.java:129) 在com.cloudbill.order.bo.OrderMain.main(OrderMain.java:174)
答案 0 :(得分:1)
尝试以下代码
java.sql.Date sqlCreatedDate = new java.sql.Date(order.getCreatedDate().getTime());
java.sql.Date sqlUpdatedDateDate = new java.sql.Date(order.getUpdatedDate().getTime());
prepairedSt.setDate(5,sqlCreatedDate);//
prepairedSt.setDate(6, sqlUpdatedDateDate);//