2015-06-15 52 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

的日期必须使用引号