2015-06-15 49 views
0

我試圖用Java和JDBC時間戳查找表中的SQL,但是當我插入時間戳到查詢字符串我在小時圖中得到一個錯誤的語法時才:在JDBC查詢字符串中的SQL語法錯誤?

String queryString = "select " + tag + " from WATER_RUNTIME_VALUES 
where Time_Stamp = 2015-06-150 8:58:00.0000000"; 

ResultSet rs = statement.executeQuery(queryString); 

這裏是錯誤:時間值

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '10'. 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.executeQuery(SQLServerStatement.java:616) at bandon.scada.core.InitConn.calculateHourlyTotal(InitConn.java:58) at bandon.scada.core.InitConn.dbConnect(InitConn.java:36) at bandon.scada.core.InitConn.main(InitConn.java:24)

+0

什麼是標籤的內容? – Jens

+1

考慮使用參數化查詢。這將避免需要將日期文字放在引號中並格式化日期文字字符串。 –

回答

3

試試這個..

String queryString = "select ' " + tag + " ' from WATER_RUNTIME_VALUES where Time_Stamp = '2015-06-150 8:58:00.0000000'"; 
0

將單引號轉義空格中2015-06-150 8:58:00.0000000

String queryString = "select " + tag + " from WATER_RUNTIME_VALUES where Time_Stamp = '2015-06-150 8:58:00.0000000'"; 

或者更好地利用PreparedStatement

0

日期參數需使用引號,

String queryString = "select " + tag + " from WATER_RUNTIME_VALUES where Time_Stamp = '2015-06-150 8:58:00.0000000'" 
-1

的日期必須使用引號