0

A user is reporting my service is returning him error, reporting following exception:

Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'lastRegResult' at row 1

Error Code: 1406

The column in question is defined as VARCHAR(120), and it is quite possible the data I am attempting to store there are sometimes longer. I expected the data will be silently truncated, but it seems I was wrong.

I could shorten the data to 120 characters before calling setLastRegResult, but I do not want to have a hardcoded constant 120 in my code, so that I do not have to update it if the database column size will change. How can I do this cleanly?

A solution might be:

  • how can I read the size of the column so that I can truncate it myself?
  • or, how I can tell the JPA to perform the truncation for me?
Suma
  • 33,181
  • 16
  • 123
  • 191
  • 1
    I can't imagine a scenario where quietly truncating the data is better than knowing the maximum size of the data and forbidding the user from exceeding it. Sounds like an awful user interface! – Duncan Jones Feb 15 '13 at 08:44
  • @DuncanJones The field `lastRegResult` is storing the error message for debugging purposes. It is better for me to get a part of the error message than to get none. It is not important for me to get the whole error message. – Suma Feb 15 '13 at 08:45
  • Check this it returns data of a certain field: http://stackoverflow.com/questions/5093551/how-to-return-field-type-from-mysql-query – Zeina Feb 15 '13 at 08:46

0 Answers0