我正嘗試使用Java將數據插入Microsoft SQL數據庫。我已經建立的連接,但是當我嘗試運行以下命令:Microsoft SQL Server與Java的日期問題
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在做什麼 – CharlesMighty 2014-11-14 19:51:58
當然,我相信我把它放在第一塊代碼中。如果沒有,請告訴我。 – user2078519 2014-11-14 19:55:16
你能打印整個SQL語句而不是嘗試執行嗎? – 2014-11-14 19:55:53