2

I am working with JDBC template(SPRING) which wrraps JDBC. during runtime all my variables values (bind variables) are held as strings(with some recognition of their actual type: bigint\varchar etc.). While I'm using setObject, I'm not sure if I need to cast the variable value to it's real type or I can send the variable as string to setObject and the database will convert it according to the column name in the database(i.e. if it is compared with BigInt then it will convert the string to big int and then query and etc.)

Thanks,

7
  • Which version of Spring? Commented Aug 3, 2015 at 15:28
  • Currently 4.2. why does it matter? Commented Aug 3, 2015 at 15:29
  • Probably not, just want to make sure its not a very old version before I provide an answer. Commented Aug 3, 2015 at 15:30
  • On which class are you calling "setObject"? Commented Aug 3, 2015 at 15:35
  • Im not really using setObject, because i use SPRING API: public <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse) throws DataAccessException hope it makes sense Commented Aug 3, 2015 at 15:38

1 Answer 1

1

If I am understanding your question correctly then I think you CAN NOT use setObject to pass a String that will be converted to a non-String SQL type (int, bigint).

Look at this question. Below is the answer from that question.

It is not the job of setObject to determine the correct conversion to the column type. The setObject javadoc says, "The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database." So it is solely looking at the Java type of the object passed to it and converting that to a SQL type. So you pass it a String and it converts it to a varchar which is appropriate. If you want setObject to do a conversion to a different type, that is the reason for the additional setObject variant which takes a target sql type to convert to, but that doesn't help your situation where you don't know what the target type is.

Sign up to request clarification or add additional context in comments.

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.