Subject | RE: [Firebird-Java] Re: MD5 in VARCHAR |
---|---|
Author | Robert DiFalco |
Post date | 2003-07-02T21:57:25Z |
Too bad the JDBC designers did not add methods to go along with
setString, getString, etc that take a character encoding argument.
I agree with removing the encoding. If for no other reason than it makes
#setBytes symetrical with #getBytes. Currently, when you write the MD5
with CHARACTER SET OCTETS, the bytes will go in as one value and come
out as another. For example, try something like this:
con.setAutoCommit( true );
con.executeUpdate( "CREATE TABLE Test(f_md5 VARCHAR(16) CHARACTER SET
OCTETS);" );
PreparedStatement ps =
con.prepareStatement( "INSERT INTO Test(f_md5) VALUES (?);" );
String s = "test string";
MessageDigest md = MessageDigest.getInstance( "MD5" );
byte data[] = md.digest( s );
s.setBytes( 1, data );
ps.execute();
ps = con.prepareStatement( "SELECT * FROM Test WHERE f_md5 = ?;" );
ps.setBytes( 1, data );
ResultSet rs = ps.executeQuery();
byte got[] = rs.getBytes( 1 );
assertTrue( Arrays.equals( data, got ) ); // UH OH!
Odd, it finds the correct record, but the data retrieved is different
than what was put in and searched for!
R.
-----Original Message-----
From: Roman Rokytskyy [mailto:rrokytskyy@...]
Sent: Wednesday, July 02, 2003 2:43 PM
To: Firebird-Java@yahoogroups.com
Subject: [Firebird-Java] Re: MD5 in VARCHAR
encoding is UNICODE_FSS... I'm afraid there's no chance to fool it.
The only choice for you would be to modify line
field.sqldata = field.encodeString(value,javaEncoding);
into
field.sqldata = value;
in body of FBStringField.setBytes(int, byte[]) method.
Sorry.... I will modify version in HEAD branch, but if you use 1.0
release, you should fetch release version from the CVS.
Blas, should we consider this a bug or a feature? Currently
FBStringField.setBytes(byte[]) is used only in
FBStringField.setBinaryStream(InputStream) method. I see no point to
force encoing in that method. Also it is not consistent with
FBStringField.getBytes() method. What do you think?
Best regards,
Roman Rokytskyy
Yahoo! Groups Sponsor
To unsubscribe from this group, send an email to:
Firebird-Java-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
setString, getString, etc that take a character encoding argument.
I agree with removing the encoding. If for no other reason than it makes
#setBytes symetrical with #getBytes. Currently, when you write the MD5
with CHARACTER SET OCTETS, the bytes will go in as one value and come
out as another. For example, try something like this:
con.setAutoCommit( true );
con.executeUpdate( "CREATE TABLE Test(f_md5 VARCHAR(16) CHARACTER SET
OCTETS);" );
PreparedStatement ps =
con.prepareStatement( "INSERT INTO Test(f_md5) VALUES (?);" );
String s = "test string";
MessageDigest md = MessageDigest.getInstance( "MD5" );
byte data[] = md.digest( s );
s.setBytes( 1, data );
ps.execute();
ps = con.prepareStatement( "SELECT * FROM Test WHERE f_md5 = ?;" );
ps.setBytes( 1, data );
ResultSet rs = ps.executeQuery();
byte got[] = rs.getBytes( 1 );
assertTrue( Arrays.equals( data, got ) ); // UH OH!
Odd, it finds the correct record, but the data retrieved is different
than what was put in and searched for!
R.
-----Original Message-----
From: Roman Rokytskyy [mailto:rrokytskyy@...]
Sent: Wednesday, July 02, 2003 2:43 PM
To: Firebird-Java@yahoogroups.com
Subject: [Firebird-Java] Re: MD5 in VARCHAR
> Oddly enough. It seems to require 28 OCTETS characters to hold 16Hmmm... that's a "joke" of our encoder because your connection
> bytes.
> Not sure how that match works out.
> ...
> OCTETS works but I think there is a problem with calculating the
> field length. If the field is 16 characters (the length of an MD5) I
> get a data truncation exception while 32 works. Maybe it splits each
> byte into a character of 128 bits instead of 256?
encoding is UNICODE_FSS... I'm afraid there's no chance to fool it.
The only choice for you would be to modify line
field.sqldata = field.encodeString(value,javaEncoding);
into
field.sqldata = value;
in body of FBStringField.setBytes(int, byte[]) method.
Sorry.... I will modify version in HEAD branch, but if you use 1.0
release, you should fetch release version from the CVS.
Blas, should we consider this a bug or a feature? Currently
FBStringField.setBytes(byte[]) is used only in
FBStringField.setBinaryStream(InputStream) method. I see no point to
force encoing in that method. Also it is not consistent with
FBStringField.getBytes() method. What do you think?
Best regards,
Roman Rokytskyy
Yahoo! Groups Sponsor
To unsubscribe from this group, send an email to:
Firebird-Java-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.