【问题标题】:Microsoft SQL Server Date issue with JavaJava 的 Microsoft SQL Server 日期问题
【发布时间】:2014-11-14 19:48:22
【问题描述】:

我正在尝试使用 Java 将数据插入 Microsoft SQL 数据库。我已经建立了连接,但是当我尝试运行以下命令时:

        stmt = con.createStatement();
        stmt.executeUpdate("INSERT INTO ctf_data "+
        "(ServerAddress,ServerName,HostName,"+
        "UserClaimedServerName,ClaimedDate,CorrectDate,"+
        "isReachable,HTTPUp,HTTPStatus,WebServer,"
        +"poweredBy) VALUES ("+ item.serverAddress +","
        +item.serverName+","+item.hostName+","+
        item.claimedHostName+","+dateTime+","+
        correctDateTime+","+item.isReachable+","+
        item.HTTPWorking+","+item.httpStatusCode+","+
        item.websrv+","+item.poweredBy+")");
        stmt.close();

我得到了例外:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '.0'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:792)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:642)
at SQLInteractor.write(SQLInteractor.java:68)
at status1.recvStatus(SQLInteractor.java:21)
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 com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)
at com.google.common.eventbus.EventBus.post(EventBus.java:275)
at ServerCheck.run(ServerCheck.java:153)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我已经建立了字符串,一切似乎都是正确的。这是我的表配置:

我也尝试过 datetime 类。我通过以下方式生成日期/时间戳:

Timestamp dateTime = new Timestamp(item.dateTime_d.getTime());
Timestamp correctDateTime = new Timestamp(item.correctDateTime_d.getTime());

这种方法给我的日期戳是这样的:2014-11-14 14:35:06.0

这是打印出来的SQL语句:

INSERT INTO ctf_data (ServerAddress,ServerName,HostName,UserClaimedServerName,ClaimedDate,CorrectDate,isReachable,HTTPUp,HTTPStatus,WebServer,poweredBy) VALUES (192.168.0.4,WIN2012SERV,WIN2012SERV,WIN2012SERV,2014-11-14 14:39:37.0,2014-11-14 14:39:42.87,true,true,200,[Microsoft-IIS/8.0],[ASP.NET, PHP/5.6.0])

有人知道我在这里做错了什么吗?从异常来看,它似乎不喜欢毫秒,但我不知道如何解决这个问题。

谢谢!

【问题讨论】:

  • 你能告诉我们stmt.executeUpdate的代码是什么样的吗,我想看看executeUpdate在做什么
  • 当然,我相信我把它放在了第一个代码块中。如果没有,请告诉我。
  • 能否打印整个 SQL 语句而不是尝试执行?
  • 刚刚添加了打印出来的SQL语句。

标签: java sql sql-server database


【解决方案1】:

问题在于引用,你需要像这样引用字符串类型的数据

INSERT INTO ctf_data (ServerAddress,ServerName,HostName,UserClaimedServerName,
                 ClaimedDate,CorrectDate,isReachable,HTTPUp,HTTPStatus,
                    WebServer,poweredBy) 
VALUES ('192.168.0.4','WIN2012SERV','WIN2012SERV','WIN2012SERV',
        '2014-11-14 14:39:37.0','2014-11-14 14:39:42.87','true',
        'true',200,'[Microsoft-IIS/8.0]','[ASP.NET, PHP/5.6.0]');

【讨论】:

    【解决方案2】:

    您应该为 varchar 和 text 列添加单引号。

    【讨论】:

    • 以及日期时间列。
    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    相关资源
    最近更新 更多