MySQLSyntaxErrorexception On Alter Table ...在MySQL Connector / J中转换为...
-
11-12-2019 - |
题
使用MySQL-Connector-Java-5.1.28运行Ubuntu 14.04。
让我们创建一个表:
CREATE TABLE mytable (value1 VARCHAR(32000), value2 TEXT, value3 VARCHAR(1000), value4 VARCHAR(32000)) CHARACTER SET latin1 COLLATE latin1_swedish_ci;
.
通过stategy.execute()失败地执行以下alter table语句,说“列长度太大.. * ”。
解决方案
In my previous response I have not taken into account CONVERT TO
behavior, so I have tried your test, first using the mysql native client and all works fine, column are automatically converted, and then using a jdbc connector and actually I got the same error... but after some test and some research I have found the solution.
You have to add in your jdbc connection url the parameter jdbcCompliantTruncation
and set it to false
jdbc:mysql://localhost/test?jdbcCompliantTruncation=false
Should the driver throw java.sql.DataTruncation exceptions when data is truncated as is required by the JDBC specification when connected to a server that supports warnings (MySQL 4.1.0 and newer)? This property has no effect if the server sql-mode includes STRICT_TRANS_TABLES.
Default: true
Source: 5.1 Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J
Running my java code using the above connection url the table mytable
are correctly converted (I have tried only with an empty table):
mysql> desc mytable;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| value1 | mediumtext | YES | | NULL | |
| value2 | mediumtext | YES | | NULL | |
| value3 | varchar(1000) | YES | | NULL | |
| value4 | mediumtext | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
So, if the server runs without STRICT_TRANS_TABLES
in sql_mode
, ex:
mysql> show variables like 'sql_mode';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| sql_mode | NO_ENGINE_SUBSTITUTION |
+---------------+------------------------+
the jdbcCompliantTruncation=false
cause the alignment of the behavior between server and jdbc client.