I am trying to create a prepared query wherein the unit part of the date needs to be passed as a parameter. Below is the code
PreparedStatement ps = conn.prepareStatement("select DATE_SUB(UTC_TIMESTAMP(),INTERVAL ? ?)");
ps.setInt(1,timeInterval);
ps.setString(2,unit);
Note: timeInterval is an int variable & unit is a String variable which can be "DAY","MINUTE" and so on.
Using this final query build as
select DATE_SUB(1511846832475,INTERVAL 2 'DAY')
But when I run it the following exception is thrown.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Day')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)