Philippe
Philippe

Reputation: 2029

Automatically trim string if it is too long on insert/update

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)

Upvotes: 1

Views: 3389

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136082

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);
}

Upvotes: 2

Related Questions