我有一張桌子。MySQL自動截斷不能與c3p0一起使用
mysql> SELECT @@GLOBAL.sql_mode;
...返回空。
mysql> create table testtable (name varbinary(5));
mysql> insert into testtable values('hellowhowareyoudsafafas');
Query OK, 1 row affected, 1 warning (0.00 sec)
將數據插入表中。但是如果我使用c3p0的ComboPoolDataSource
,我會得到例外com.mysql.jdbc.MysqlDataTruncation
。
因爲我沒有在MySQL 5.1上設置MySQL嚴格模式,所以我期待數據存儲在表中。我應該怎麼做才能通過c3p0自動截斷。
=====確定我做到了,但有一個更合適的方法======
PreparedStatement ps1 = connection.prepareStatement("SELECT @@sql_mode");
ResultSet rs = ps1.executeQuery();
String originalSqlMode = "";
if(rs.next()) {
originalSqlMode = rs.getString(1);
System.out.println(originalSqlMode);
}
String newSqlMode = originalSqlMode.replace("STRICT_ALL_TABLES", "").replace("STRICT_TRANS_TABLES", "").replace(",,", ",");
ps1 = connection.prepareStatement("SET SESSION sql_mode=?");
ps1.setString(1, newSqlMode);
ps1.execute();
PreparedStatement ps2 = connection.prepareStatement("insert into testtable values (?)");
ps2.setString(1, "indianpeople");
ps2.execute();
這裏有一個我的想法,直到你得到一個正確的答案......即使在MySQL中,你也會得到一個警告。我認爲抓住'MysqlDataTruncation'將是忽略警告的Java等價物。 – 2013-02-25 10:38:17