Saturday, 26 November 2016

Convert from java.util.Date to java.sql.Date

I am writing a programm for creating schedules for a bunch of users.
Now I have a controller called AddTermineController which should allow the end-user to create a schedule for another user and also a database connection class for schedules.
When trying to save the schedule I am getting this error:



java.util.Date cannot be cast to java.sql.Date



This is out of the database class where I have created the statements:


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();
}
}

This is the error code I am given:



Exception in thread "JavaFX Application Thread"
java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException at
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) at
javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at
com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at 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)
at
com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at
com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at
com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at
com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at
com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at
com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at
com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at 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
javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at
javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at
com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at
com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at
com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at 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)
at java.lang.Thread.run(Unknown Source) Caused by:
java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
sun.reflect.misc.Trampoline.invoke(Unknown Source) at
sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
sun.reflect.misc.MethodUtil.invoke(Unknown Source) at
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) ...
48 more Caused by: java.lang.ClassCastException: java.util.Date cannot
be cast to java.sql.Date at
de.dpma.azubidpma.dao.TerminDAO.addTermin(TerminDAO.java:82) at
de.dpma.azubidpma.view.AddTermineController.saveButton(AddTermineController.java:79)
... 58 more



If you need any other code brackets let me know, I will update this post as quick as possible.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...