0

I have a MySQL table (simplified):

CREATE TABLE `tokens` (
    `token` BINARY(16) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And I try to insert a row like this:

JdbcTemplate jdbcTemplate; // org.springframework.jdbc.core.JdbcTemplate
...
String sql = "INSERT INTO `tokens` (`token`) VALUES (?)";
String token = "123e4567e89b12d3a456426655440000"; // UUID
jdbcTemplate.update(sql, new Object[]{token.getBytes()});

But I get this exception:

Data truncation: Data too long for column 'token' at row 1  

What do I do wrong? Thanks.

Edit: see my answer, I missed the hexadecimal conversion (token is an UUID).

3
  • "Data truncation: Data too long for column 'token' at row 1 " Commented Sep 21, 2016 at 3:32
  • increase size of column token . change token BINARY(16) to token BINARY(32) Commented Oct 5, 2016 at 4:09
  • @Arunkumar I was missing the hexadecimal conversion, a 32 chars UUID contains 16 hexadecimal codes. Commented Oct 5, 2016 at 14:04

1 Answer 1

1

Here is the solution to store the token (which is an UUID):

jdbcTemplate.update(sql, new Object[]{DatatypeConverter.parseHexBinary(token)});

Let us know if there are other ways...

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

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.