简体   繁体   中英

JDBC API to trim String length to match with database column specification

My input String is much longer than what database column specification is. Do we have any API in jdbctemplate to automatically trim String to match with database column limit.
We are not yet sure, what would be the limit in this column as this may change in future and even it may vary according to environment. So limiting String in my code is not a feasible solution, as I want to store as much as possible.

No.
That should be part of your input validation.
Please note that in any case you should never trust client side, so a client-side input validation (ie putting maxlength on your input field) is required but it's not enough.
You should also perform a server side input validation, and in any case you should not allow an input which exceed the database column limit.

  • To achieve this we can use the trim/substring database functions in the query we are writing.
  • This is not good idea to trim the data user provided. If you can't change the table then please enter data in the multiple rows.

If you are using JPA you can try Descriptor PreInsert/PreUpdate event for this, or possibly just use JPA PreInsert and PreUpdate events. You have to just check the size of the fields and truncate them in the event code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM