0

I'm trying to search a table in sql by timestamp using java and jdbc, but when I insert the timestamp into the query string I get a sytax error at the hour figure:

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

 ResultSet rs = statement.executeQuery(queryString);

And here is the error:

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)

2
  • What is the content of tag? Commented Jun 15, 2015 at 10:58
  • 1
    Consider using parameterized queries. That will avoid the need to put date literals in quotes and format date literal strings. Commented Jun 15, 2015 at 11:04

4 Answers 4

3

Try this..

String queryString = "select ' " + tag + " ' from WATER_RUNTIME_VALUES where Time_Stamp = '2015-06-150 8:58:00.0000000'";
Sign up to request clarification or add additional context in comments.

Comments

0

Use quotes in date parameters,

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

Comments

0

Put single quotes in time value to escape the spaces in 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'";

Or better use PreparedStatement

Comments

-1

The date needs to be within quote marks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.