Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error when using prepareStatement with interval in query

When running this query SELECT SYSDATE + INTERVAL '7' DAY FROM DUAL; in a prepareStatement like this

    PreparedStatement ps =  connection.prepareStatement("select sysdate + interval ? day from dual" );      
    ps.setString(1, "7");
    ps.executeQuery();

It will throw an exception, that the syntax is not good, it clearly is, cuz i'm able to run the same query in sql-developer.

Is this a bug in PreparedStatement ? can i use prepared statements together with interval?

like image 327
Elye M. Avatar asked Mar 15 '15 20:03

Elye M.


People also ask

What are the restrictions of a PreparedStatement in SQL?

Restriction: For a PreparedStatement that contains an IN predicate, the expression that is the argument of the IN predicate cannot have more than 32767 parameters if the target data server is a Db2® on Linux®, UNIX, and Windows systems system. Otherwise, the IBM Data Server Driver for JDBC and SQLJ throws an SQLException with error code -4499.

How to fix Safari ‘error performing query’ error?

Now close the settings window and click on ‘ Develop ‘ in the title menu at the top of your screen. You will now be shown a sub-menu. Scroll down and click on ‘ Empty Caches ‘. This will clear all the cache files from Safari which should solve the ‘ Error performing query ‘ issue for you.

How do I keep errors in a column?

This is where Keep errors can be helpful. To keep rows that have errors, first select the column that contains errors. On the Home tab, in the Reduce rows group, select Keep rows. From the drop-down menu, select Keep errors.

How do I fix the ‘error performing query’ issue?

In case you are still facing the ‘Error performing query’ issue despite trying the fixes above, we recommend you to uninstall and reinstall the app on your device. A fresh installation can fix overlapping codes and conflicts with other services in the background which should help solve the ‘Error performing query’ issue for you.


Video Answer


1 Answers

The entire expression INTERVAL '7' DAY is a literal, you cannot simply replace a part of it with a variable (parameter). Use the function NUMTODSINTERVAL(?,'DAY') instead.

like image 141
mustaccio Avatar answered Oct 15 '22 14:10

mustaccio