0

I have a byte array as shown below,

byte b1[] = {-127, -87, -32, -112};

when I'm trying to convert it to a String it's showing black diamond like character, I want to insert it in the database.

Which charset should I use for database?

I have tried utf8mb4 and CP1250? I'm using mysql version 5.7.14 ?

Given below error while inserting:

java.sql.SQLException: Incorrect string value: '\xEF\xBF\xBD\xEF\xBF\xBD...' for column

Here is the code:

   byte b1[] = {-127, -87, -32, -112};
   TRNRequest tr = new TRNRequest();
   tr.set(new String(b1));
   trnRequestService.saveTransactionRequest(tr);

trnRequestService is dao layer service which stores object in database and TRNRequest is my class, I am using Hibernate for database connection. I want to read this character

public class DbByteInsert {
        public static void main(String[] args) {
             System.out.println(new String(new byte[]{ 48, 50}));
             System.out.println(new String(new byte[]{ -127, -87, -32, -112}));
        }
}

check this example I am able to insert first byte array in db but I can not insert second..!

19
  • 1
    How can anyone guess ur encoding, provide atleast code where from you got this? Commented Nov 30, 2016 at 9:52
  • Please provide your code here. Commented Nov 30, 2016 at 9:53
  • How you are converting byte[] to string. Use Arrays.toString(b1) to convert array into string and insert the resulted string into database Commented Nov 30, 2016 at 9:55
  • I am using UTF8 charset Commented Nov 30, 2016 at 10:04
  • 1
    Getting black diamonds means that the bytes cannot be decoded to a string. You're losing data when you convert to string. What are you trying to do? If you want to insert binary data into a database, you can, just don't try to convert to a string. Commented Nov 30, 2016 at 10:14

0

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.