'How to register custom type to type mapping
I want to register a custom type to JDBC so that when I call setObject in PreparedStatement, it can automatically recognize this type and know how to serialize type to SQL data.
I've got some clues from this tutorial https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html#implementing_sqldata, but there are still a few points not clear to me:
what I want to register is
org.locationtech.jts.geom.Polygon, which is a type from third party library, so implementingSQLDataseems not a good idea for me, since I don't have access to class source codeIn the tutorial example, the type is registered per connection, is there any way to register this mapping globally?
Solution 1:[1]
Postgres JDBC does not support SQLData but you can use org.postgresql.util.PGobject. There are several reference implementation e.g. org.postgresql.geometric.PGpolygon. It is still required to register per connection. Imho this approach has no sense for custom types in postgres. You should implement conversation from object to string and from string to object and use setString/getString methods in JDBC API. E.g. for custom type string pair create type string_pair AS (first text, second text); Postgres string will be: "('string1', 'string2')" (do not forget to escape symbols in this string). Later you can write: "insert into ...(?::string_pair, ..." and set value by preparedStmt.setString(1, convertToStr(myPair));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sibnick |
