我有一个尝试创建的约会计划应用程序

时间:2019-07-31 19:19:41

标签: java mysql

我停在了一个需要在15分钟之内通知用户他们约会的地点。当我运行代码时,出现SQL错误。任何帮助表示赞赏。

我已经实时地代替了变量,并且工作正常。当我把变量放回去时,出现下面列出的错误。

public static Appt apptIn15Min() {
Appt appointment;
LocalDateTime now = LocalDateTime.now();
ZoneId zid = ZoneId.systemDefault();
ZonedDateTime zdt = now.atZone(zid);
LocalDateTime ldt = zdt.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
LocalDateTime ldt2 = ldt.plusMinutes(15);
String user = UserDB.getCurrentUser();
System.out.println(ldt);
System.out.println(ldt2);
System.out.println(user);
try {
    Statement statement = DBConnection.getConnection().createStatement();
    String query = "SELECT * FROM appointment WHERE start BETWEEN '" + ldt + "' AND '" + ldt2;
    ResultSet results = statement.executeQuery(query);
    System.out.println(query);
    System.out.println("Lookup started");
    if(results.next()) {
        appointment = new Appt(results.getInt("aptId"), results.getInt("customerId"), results.getString("start"),
            results.getString("end"), results.getString("contact"), results.getString("type"),
            results.getString("location"), results.getString("contact"));
        System.out.println("Lookup Completed");
        return appointment;
    }
} catch (SQLException e) {
    System.out.println("SQLException: " + e.getMessage());
}

错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)的sun.reflect.NativeConstructorAccessorImpl的第1行的``2019-07-29T21:59:28.709''附近使用com.mysql.jdbc.Util上的java.lang.reflect.Constructor.newInstance(Constructor.java:423)上的.newInstance(NativeConstructorAccessorImpl.java:62)在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)处。 com.mysql.jdbc.Util.getInstance(Util.java:408)的com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)的com.mysql.jdbc.MysqlIO的handleNewInstance(Util.java:425) com.mysql.jdbc上的.checkErrorPacket(MysqlIO.java:3978)。com.mysql.jdbc上的com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)上的.checkErrorPacket(MysqlIO.java:3914)。 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2491)处的mysqlIO.sqlQueryDirect(MysqlIO.java:2683)com.mysql.jdbc.ConnectionImpl.execSQL(Connect ionImpl.java:2449)位于com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)位于Builds.ApptDB.apptIn15Min(ApptDB.java:216)位于Interface.MainController.initialize(MainController.java:135) javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)在javafx.fxml.FXMLLoader javafx.fxml的.loadImpl(FXMLLoader.java:3175).javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)处的javafx.fxml.FXMLLoader.loadImpl(FXMLLoader。 javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)处的java:3104)sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)处的interface.LoginController.tryLogin(LoginController.java:72)处sun.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:498)a在java.lang.reflect上的sun.reflect.GeneratedMethodAccessor1.invoke(未知源)处的sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)在java.lang.reflect处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在javafx.fxml处的sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)处的.Method.invoke(Method.java:498)在javafx.fxml处的javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769)处。 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)的FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)的FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)在com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)的com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(EventHandlerManager.java:191)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent (BasicEventDispatcher.java:58)位于com.sun.javafx.event.EventDispatchChain com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)处的com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)处的Impl.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 com.sun.javafx.event.EventUtil.fireEvent(.java:74)(javafx.event.Event.fireEvent(Event.java:198),javafx.scene.Node.fireEvent(Node。 com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)处com.sun.javafx的javafx.scene.control.Button.fire(Button.java:185)处的java:8411) com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)在com.sun.javafx.event上的.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89) 。组合teEventHandler $ 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.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)的com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(EventHandlerManager.java:191)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent (com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)上的(BasicEventDispatcher.java:58)com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)上com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)。 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)的com.sun.javafx.event.EventDispatchChainImpl.dispatchEv的javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)的ent(EventDispatchChainImpl.java:114)在javafx.event的com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)的ent(EventDispatchChainImpl.java:114) .Event.fireEvent(Event.java:198)在javafx.scene.Scene $ MouseHandler.process(Scene.java:3757)在javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485)在javafx.scene .scene.impl_processMouseEvent(Scene.java:1762)位于com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:394 )在com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda的com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)处com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)的$ handleMouseEvent $ 353(GlassViewEventHandler.java:432)com.sun.javafx.tk.quantum.GlassViewEventHandl com.sun.glass.ui上的er.handleMouseEvent(GlassViewEventHandler.java:431)。com上com.sun.glass.ui.View.notifyMouse(View.java:937)的View.handleMouseEvent(View.java:555) .com.sun.glass.ui.win.WinApplication.lambda $ null $ 147(WinApplication.java:177)的java.lang.Thread.run(Thread.sun.glass.ui.win.WinApplication._runLoop(本机方法) .java:748)

2 个答案:

答案 0 :(得分:3)

PreparedStatement

除了Safeer Ansari的回答外,我将举一个示例说明如何在这种情况下使用PreparedStatement以及它为什么会有优势。

对于LocalDateTime自变量,请使用PreparedStatement::setObject

StatementString可以这样重写:

Connection con = DBConnection.getConnection();
String query = "SELECT * FROM appointment WHERE start BETWEEN ? AND ? ;";
PreparedStatement statement = con.prepareStatement(query)
statement.setObject(1,ldt);
statement.setObject(2,ldt2);
ResultSet rs = statement.executeQuery();

请注意,如何构建String query语句不再需要任何String串联,而是可以使用?作为将变量设置为的位置。使用动态查询时,与PreparedStatement相比使用Statement没有缺点。

注意:为使示例简单,我没有包括任何资源关闭或exception处理。使用try-with-resources来完成这项工作。

答案 1 :(得分:2)

错误出现在您的查询字符串中,因为在 ldt2

之后没有结束的单引号

这是您查询的外观:

String query = "SELECT * FROM appointment WHERE start BETWEEN '" + ldt + "' AND '" + ldt2 + "'";

此外,我建议使用String.format()进行字符串格式化,而不是尝试将这些复杂的字符串连接起来。

我希望这可以解决您的问题