getObject() autoDeserialize question
"Pavel B. Milovantsev" <[email protected]> Tue, 06 Nov 2007 22:21:49 +0600
| Newsgroups | gmane.comp.db.mysql.java |
|---|---|
| Organization | SupportWizard |
| Message-ID | <[email protected]> |
Hello, not sure that this is a bug, so prefer to ask it at the maillist.
As I see the only backport at 3.1.11 which consider blobs
(http://bugs.mysql.com/bug.php?id=13277), but I don't understand why it
provoke change at ResultSet(Impl).java:getObject():
from:
if ((data[0] == -84) && (data[1] == -19)) {
// Serialized object?
....
}
to:
if ((data[0] == -84) && (data[1] == -19)) {
// Serialized object?
....
} else {
return getString(columnIndex);
}
This simple code works on mysql connector/J version 3.1.10, and
fails at version 5.1.5 with java.lang.ClassCastException:
java.lang.String cannot be cast to [B
import java.sql.*;
public class Main {
public static void main(final String[] args) throws Exception {
byte[] byteArrayWrite = new byte[Math.abs(Byte.MIN_VALUE) +
Math.abs(Byte.MIN_VALUE) + 1];
for (int b = 0; b < Math.abs(Byte.MIN_VALUE) +
Math.abs(Byte.MAX_VALUE) + 1; b++)
byteArrayWrite[b] = (byte)(b + Byte.MIN_VALUE);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/sw2_std?user=root&password=XXX&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoDeserialize=true&jdbcCompliantTruncation=false");
PreparedStatement stmt = conn.prepareStatement("insert into
testblob(blob_field) values(?)");
stmt.setObject(1, byteArrayWrite);
stmt.executeUpdate();
stmt = conn.prepareStatement("select blob_field from testblob");
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
final byte[] byteArrayRead = (byte[]) rs.getObject(1);
// Make sure original and read content are equal
assert byteArrayRead.length == byteArrayWrite.length;
for (int byteArrayIdx = 0; byteArrayIdx < byteArrayRead.length;
byteArrayIdx++)
assert byteArrayRead[byteArrayIdx] == byteArrayWrite[byteArrayIdx];
}
}
}
> mysql> show create table testblob;
> +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | Table | Create Table |
> +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> | testblob | CREATE TABLE `testblob` (
> `pk_field` bigint(20) NOT NULL auto_increment,
> `blob_field` mediumblob,
> PRIMARY KEY (`pk_field`)
> ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
> +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
> 1 row in set (0.00 sec)
--
Pavel B. Milovantsev <[email protected]>
SupportWizard
--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/[email protected]