尝试将数据插入数据库表时,无法将java.util.Date强制转换为java.sql.Date

时间:2015-03-06 05:21:36

标签: java sql jdbc

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)

1 个答案:

答案 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);//