截断字符串
我正在使用第三方用户数据,这些数据可能适合也可能不适合我们的数据库。如果数据太长则需要截断。
我们将 IBatis 与 Connector/J 一起使用。如果数据太长,则会抛出 SQL 异常。我有两个选择:要么截断Java中的字符串,要么使用子字符串截断sql中的字符串。
我不喜欢截断 sql 中的字符串,因为我在 Ibatis XML 中编写表结构,但另一方面 SQL 知道我们的数据库排序规则(它不一致,并且要保持一致会很昂贵)并且可以截断以多字节安全方式的字符串。
有没有办法让 Connector/J 直接插入此 SQL,如果没有,人们会推荐哪条路线?
I'm working with third party user data that may or may not fit into our database. The data needs to be truncated if it is too long.
We are using IBatis with Connector/J. If the data is too long a SQL exception is thrown. I have had two choices: either truncate the strings in Java or truncate the strings in sql using substring.
I don't like truncating the strings in sql, because I am writing table structure in our Ibatis XML, but SQL on the other hand knows about our database collation (which isn't consistent and would be expensive to make consistent) and can truncate string in a multibyte safe manner.
Is there a way to have the Connector/J just straight insert this SQL and if not which route would people recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MySQL 文档,插入的数据可能超过长度可以被视为警告:
Connector/J 属性之一是
jdbcCompliantTruncation
。这是其描述 :如果我理解正确,那么将此属性设置为 false 不会引发异常,而是插入截断的数据。此解决方案不需要您截断程序代码或 SQL 语句中的数据,而是将其委托给数据库。
According to the MySQL documentation it's possible that inserting data that exceeds the length could be treated as a warning:
One of the Connector/J properties is
jdbcCompliantTruncation
. This is its description:If I understand correctly then setting this property to false doesn't throw the exception but inserts the truncated data. This solution doesn't require you to truncate the data in program code or SQL statements, but delegates it to the database.