1

I've got an application which gets data from the web and inserts it into a H2 DB. On rare occasions, the value being inserted is bigger then the SQL column size. In these cases I'd like to automatically trim the end of the value (at least for strings). I don't care about data loss as it only concerns informational values shown to the end user.

Info:
- For MySQL I've found the STRICT parameter (Automatically trimming length of string submitted to MySQL). Is there an equivalent parameter for h2 I didn't find?
- I'd like to avoid having to check the string length in code as it potentially affects many fields.

System:
- OS: win/osx/linux
- DB: h2 embedded 1.3.173 (could be updated to latest)

Community
  • 1
  • 1
Philippe
  • 1,949
  • 4
  • 31
  • 57

1 Answers1

2

You can make a wrapper over PreaparedStatement and truncate long strings there

...

public void setString(int i, String str) {
    str = str.length() > max ? str.substring(0, max) : str;
    target.setString(str, i);
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • This would work, but I'd like to avoid having to do this on the java side as 1) the max length is usually respected (out of 1300 queries 1 returns a string which is too long) and 2) ~40 fields would require this check – Philippe Nov 15 '13 at 10:01
  • As the magic h2 switch doesn't seem to exist, I'll go with your proposition. Thanks. – Philippe Nov 18 '13 at 07:44