简体   繁体   English

JDBC API调整字符串长度以与数据库列规范匹配

[英]JDBC API to trim String length to match with database column specification

My input String is much longer than what database column specification is. 我的输入String比数据库列规范长得多。 Do we have any API in jdbctemplate to automatically trim String to match with database column limit. jdbctemplate中是否有任何API可以自动修剪String以使其与数据库列限制匹配。
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. 因此在我的代码中限制String是不可行的解决方案,因为我想尽可能多地存储。

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. 请注意,无论如何,您都不应该信任客户端,因此需要客户端输入验证(即,在输入字段中输入maxlength),但这还不够。
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. 为此,我们可以在正在编写的查询中使用trim / substring数据库函数。
  • 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. 如果您使用的是JPA,则可以为此尝试使用Descriptor PreInsert / PreUpdate事件,或者可以仅使用JPA PreInsert和PreUpdate事件。 You have to just check the size of the fields and truncate them in the event code. 您只需要检查字段的大小并在事件代码中截断它们即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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