Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing UUID in mysql database table

I am facing a peculiar problem while storing UUID to mysql table. I have a table with a column declaration :

    expenseUUID varchar(32) primary key ,

in this column I wish to save UUID. I have generated UUID as follows:

  String expenseUUID = UUID.randomUUID().toString();
  expenseUUID.replaceAll("-", "");

But when I try to insert expenseUUID using JDBC , I get an error saying :

Data truncation: Data too long for column 'expenseUUID' 

What should I do resolve this issue? I am stuck. I am not able to figure out what is going wrong.

like image 989
user2056245 Avatar asked Oct 27 '25 10:10

user2056245


1 Answers

You just missed to reassign your replaceAll result to expenseUUID var. Then your var still contains a 36 chars string.

Current :

String expenseUUID = UUID.randomUUID().toString();
expenseUUID.replaceAll("-", "");
System.out.print(expenseUUID); // 9e73f8a4-dfde-438e-9eec-ac4e94817b6b

Corrected :

String expenseUUID = UUID.randomUUID().toString();
expenseUUID = expenseUUID.replaceAll("-", "");
System.out.print(expenseUUID); // 0757c2666e934e2eb303df68bb3c9761
like image 103
zessx Avatar answered Oct 28 '25 23:10

zessx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!