0

I need to convert a String to a GUID, there are lots of tutorials online about converting one to a UUID but not to a GUID. I am working in Java.

When I use a UUID, I get the following error:

ERROR: operator does not exist: character varying = uuid
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

I have tried using help from the following question but Guid doesn't seem to be a type in java and the constructor for GUID doesn't take any arguments:

How to try convert a string to a Guid

K. Martin
  • 73
  • 13
  • UUID is basically the Java type that represents a GUID. What are you trying to do that you can't do with UUID? – Jon Skeet Mar 23 '20 at 14:42
  • I am trying to lookup a value in a postgres database that is of type GUID – K. Martin Mar 23 '20 at 14:43
  • 2
    @K.Smith are you using any library to make use of the class `GUID`? Not really clear what exactly is the requirement here. Again, what is it that `UUID` doesn't solve for you while working with Java? – Naman Mar 23 '20 at 14:48
  • I get an error when using UUID, I'll edit the original question to show this. Sorry about the confusion! – K. Martin Mar 23 '20 at 15:00
  • 1
    Well, then your "UUID" column isn't really a `uuid` but a `varchar` column. And if everything is a String, there is nothing you need to convert –  Mar 23 '20 at 15:02

1 Answers1

3

Assuming your column is defined as uuid in Postgres, you can use java.util.UUID for that.

Something like:

java.util.UUID id = UUID.fromString("...."); 
PreparedStatement pstmt = connection.prepareStatement("select * from some_table where id = ?");
pstmt.setObject(1, id);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
  ... do something 
}
  • When I do that I get the following error: ERROR: operator does not exist: character varying = uuid Hint: No operator matches the given name and argument types. You might need to add explicit type casts. – K. Martin Mar 23 '20 at 15:00
  • 2
    @K.Smith: then your "uuid" column in the database isn't really a `uuid` but (incorrectly) defined as `varchar` - which is a bad idea. If that really is the case then this question isn't really about UUIDs but simply about passing Strings - which is no different that e.g. comparing the `first_name` column of a table with a user given value –  Mar 23 '20 at 15:02
  • My column is definitely a uuid! – K. Martin Mar 23 '20 at 15:04
  • 1
    @K.Smith: not if you get that error. Please [**edit**](https://stackoverflow.com/posts/60815740/edit) your question and add the full `CREATE TABLE` statement for the statement in question. –  Mar 23 '20 at 15:05
  • I've looked into it and you're right! I was using the generate uuid function but then saving it as a varchar. Thanks for you help! – K. Martin Mar 23 '20 at 15:13