88969

java enum and postgresql enum

Question:

CREATE TABLE customers ( first_name character varying(15), second_name character varying(20), login character varying(15) NOT NULL, password character varying(15), email character varying(40), gender gender, register_date date, date_of_birth date, address character varying(40), address_number integer, town character varying(20), CONSTRAINT login PRIMARY KEY (login) )

I have this table and I created an enum of gender such as:

CREATE TYPE gender AS ENUM ( 'F', 'M',);

I am trying to insert into customers data from eclipse java with PreparedStatement but there is an error such as ERROR: column "gender" is of type gender but expression is of type character varying Hint: You will need to rewrite or cast the expression.

My Java code looks like:

PreparedStatement pre_state; public enum gendertype { F, M; } pre_state = conn.prepareStatement("INSERT INTO" + " customers VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); pre_state.set(6, gendertype.F.toString());

Answer1:

I can take no credit for this answer as you have already solved it, but I will explain why it works.

PostgreSQL provides the answer when it says

<blockquote>

Hint: You will need to rewrite or cast the expression

</blockquote>

The Java code is creating a string literal value that represents the Java enum gendertype type.

Casting a literal to a PostgreSQL gender type is done by adding a casting suffix to the value ::gender.

So valid input would be

'F'::gender

or

'M'::gender

This works because all PostgreSQL types have a input method that takes a text representation and converts that to the internal form.

Recommend

  • sql join on two fields in one table
  • How to pass string and dictionary in NUnit test cases?
  • When an entity has more than one 0..1:n relationships to another enitity, how to define them using F
  • How to get state of SelectBooleanCheckbox in Ajax Listener?
  • Checking if URL exists - HTTP Request always returns an exception
  • Get enum value from string
  • How can I use C++ enum types like C#?
  • What is a good persistence design for this entity hierarchy?
  • Cannot style mat-tab without ::ng-deep and !important
  • Wrapping text in spinner android
  • Any nice way to generate a timeline view of commits from subversion?
  • AlertDialog style when using setView()
  • Local Development, Apache vs Developer - file permissions
  • Not able to aggregate on nested fields in elasticsearch
  • Why querying a date BC is changed to AD in Java?
  • Database structure design with variable amounts of fields
  • Why value captured by reference in lambda is broken? [duplicate]
  • PHP - How to update data to MySQL when click a radio button
  • Incrementing object id automatically JS constructor (static method and variable)
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to get icons for entities from eclipse?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Why joiner is not used after Sequence generator or Update statergy
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • Recursive/Hierarchical Query Using Postgres
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app