1

I have a type as 'designation' in my postgreSQL DB instance. I have a table named 'Prof' that has a column 'designation' of type 'designation'. I want to insert values into this column using java.

code snippet:

insProf.setInt(1, id);
insProf.setInt(2, univ_id);
insProf.setString(3, desg);
int rs_insProf = insProf.executeUpdate();

output error:

Enter designation: org.postgresql.util.PSQLException: ERROR: invalid input value for enum designation: ""

Please help with with syntax, I am not able to figure out how best I can handle this.

3
  • how your query look like? Commented Mar 29, 2017 at 6:54
  • I would try to use insProf.setObject(3, desg, Types.OTHER) Commented Mar 29, 2017 at 7:02
  • Are you sure that your designation type accepts the empty string ''? (It only accepts it, if you enumerated that too within the definition). You may want to use NULL instead of the empty string. Commented Mar 29, 2017 at 10:21

2 Answers 2

1

you can use setObject() and getObject() method.

https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setObject(int,%20java.lang.Object)

take a look at the setObject() and getObject().

according to the documentation, "If the object is of a class implementing the interface SQLData, the JDBC driver should call the method SQLData.writeSQL to write it to the SQL data stream."

so you can create your own type by creating a custom class implementing SQLData interface.

plus, don't forget to register your custom type into the driver's type map.

there is a tutorial too: https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html

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

Comments

0

To insert an enum, you have to use this syntax:

INSERT INTO Prof (id, unit_id, designation) VALUES(?, ?, {?, ?, ?});
//-------------------------------------------------------^-------^

Or

You can use an array in your case of type designation like this :

INSERT INTO Prof (id, unit_id, designation) VALUES(?, ?, ARRAY[?]::designation[]);
//-------------------------------------------------------------^

To set multiple you can use a loop for example to fill your enum, if you have more then one enum.

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.