从java.util.Date转换为java.sql.Date

时间:2017-10-13 07:23:41

标签: java sql oracle

我正在编写一个程序来为一堆用户创建日程安排。 现在我有一个名为AddTermineController的控制器,它应该允许最终用户为另一个用户创建一个计划,并为计划创建一个数据库连接类。 在尝试保存计划时,我收到此错误:

  

java.util.Date无法强制转换为java.sql.Date

这不在我创建语句的数据库类中:

public void addTermin(Termin terminXL) throws ParseException {
    try {

        PreparedStatement stat = con.prepareStatement(ADD_TERMIN);
        stat.setInt(1, terminXL.getUserID().getValue());
        stat.setString(2, terminXL.getUserNameT().getValue());
        stat.setString(3, terminXL.getKommentar().getValue());
        stat.setString(4, terminXL.getKategorie().getValue());
        //TODO von und bis in einen Datum umwandeln um es in die Datenbank einzuspeichern


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD");
        Date datumVon = (Date) simpleDateFormat.parse(terminXL.getVon().getValue());
        Date datumBis = (Date) simpleDateFormat.parse(terminXL.getBis().getValue());

        stat.setDate(5, datumVon);
        stat.setDate(6, datumBis);
        stat.setString(6, terminXL.getBis().getValue());
        stat.setString(7, terminXL.getReferat().getValue());

        stat.executeUpdate();
        log.info("Neuer Termin angelegt");

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

这是我给出的错误代码:

  

线程中的异常" JavaFX应用程序线程"   了java.lang.RuntimeException:   java.lang.reflect.InvocationTargetException at   javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774)at at   javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at   javafx.event.Event.fireEvent(Event.java:198)at   javafx.scene.Node.fireEvent(Node.java:8413)at   javafx.scene.control.Button.fire(Button.java:185)at   com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)     在   com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)at   javafx.event.Event.fireEvent(Event.java:198)at   javafx.scene.Scene $ MouseHandler.process(Scene.java:3757)at   javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485)at at   javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at   javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at at   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:381)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:417)     在   com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)     在com.sun.glass.ui.View.handleMouseEvent(View.java:555)at   com.sun.glass.ui.View.notifyMouse(View.java:937)at   com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at   com.sun.glass.ui.win.WinApplication.lambda为$ null $ 148(WinApplication.java:191)     在java.lang.Thread.run(未知来源)引起:   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.reflect.misc.Trampoline.invoke(未知来源)at   sun.reflect.GeneratedMethodAccessor2.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.reflect.misc.MethodUtil.invoke(未知来源)at   javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769)...   48更多引起:java.lang.ClassCastException:java.util.Date不能   被转换为java.sql.Date at   de.dpma.azubidpma.dao.TerminDAO.addTermin(TerminDAO.java:82)at at   de.dpma.azubidpma.view.AddTermineController.saveButton(AddTermineController.java:79)     ......还有58个

如果您需要任何其他代码括号让我知道,我会尽快更新此帖子。

1 个答案:

答案 0 :(得分:2)

java.util.Date d = new java.util.Date();
java.sql.Date sd = new java.sql.Date(d.getTime());