0

Let's say I'm getting a String from java.sql.ResultSet.getString(1). And from this string I called String.getBytes().

Now regardless of the charset used by the database where resultset was queried from, do I get the same result? i.e. If I performed the procedure across different databases (with different charsets), do I get the same result after calling getBytes?

2 Answers 2

1

Yes.

The bytes stored in the database are converted to Unicode characters in the String.

You convert the String chars to whatever bytes you specified (or the system defaulted) as the character encoding.

Database bytes -> String chars -> Your bytes
            DB encoding     Your encoding

If something is messed up in the database or the JDBC that isn't decoding the bytes in the database correctly, the String will be wrong. For example, if the bytes are some UTF-8 Encoding of Czech and it decodes them using some Western encoding or Windows standard encoding, the String will be wrong. You will see that, if you print the string, as the accented characters looking like fractions or funny character combinations.

But whatever gets into the String will be encoded according to your specified charset.

Sign up to request clarification or add additional context in comments.

2 Comments

But whatever gets into the String will be encoded according to your specified charset.(I didn't specify any charset. Or do you mean I should call String.getBytes(String charsetName) instead?? Passing the charset defined in the database)
If you don't specify a charset name to getBytes, it will use the default character encoding. You should specify an encoding if you want to.
0

Depends. Encoding of the db will play a roll, if your db encoding is say latin1 but you're saving something that is outside of it's character set (utf-8 char) it'll save giberish as value. Getting bytes of that bad value wouldn't be the same as original.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.